/** * Boot the application events. * * @return void */ public function boot() { $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); Relation::morphMap(['quran_recordings' => \Modules\Quran\Entities\QuranRecording::class]); }
/** * Boot the application events. * * @return void */ public function boot(Router $router) { $router->model('uuser', '\\Modules\\Users\\Entities\\User'); $router->model('urole', '\\Bican\\Roles\\Models\\Role'); $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); Relation::morphMap(['users' => \Modules\Users\Entities\User::class]); }
/** * Add the "has" condition where clause to the query. * * @param \Illuminate\Database\Eloquent\Builder $hasQuery * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param string $operator * @param int $count * @param string $boolean * @return \Illuminate\Database\Eloquent\Builder */ protected function addHasWhere(EloquentBuilder $hasQuery, Relation $relation, $operator, $count, $boolean) { $query = $hasQuery->getQuery(); // Get the number of related objects for each possible parent. $relationCount = array_count_values($query->lists($relation->getHasCompareKey())); // Remove unwanted related objects based on the operator and count. $relationCount = array_filter($relationCount, function ($counted) use($count, $operator) { // If we are comparing to 0, we always need all results. if ($count == 0) { return true; } switch ($operator) { case '>=': case '<': return $counted >= $count; case '>': case '<=': return $counted > $count; case '=': case '!=': return $counted == $count; } }); // If the operator is <, <= or !=, we will use whereNotIn. $not = in_array($operator, array('<', '<=', '!=')); // If we are comparing to 0, we need an additional $not flip. if ($count == 0) { $not = !$not; } // All related ids. $relatedIds = array_keys($relationCount); // Add whereIn to the query. return $this->whereIn($this->model->getKeyName(), $relatedIds, $boolean, $not); }
protected function verifyRelation(ReflectionMethod $method, Relation $relationship) { $expectedRelatedModel = $this->relatedModel === get_class($relationship->getRelated()); $expectedRelationName = get_class($relationship) === $this->type; $expectedReturnAnnotation = $this->getReturnAnnotation($method) === $this->type; return $expectedRelationName && $expectedRelatedModel && $expectedReturnAnnotation; }
/** * @param Relation $relation * @param array $excluded_ids */ public static function deleteAllRelatedExcept(Relation $relation, $excluded_ids = []) { $related = $relation->getRelated(); $key_name = $related->getKeyName(); $query = $relation->getQuery(); if (count($excluded_ids) > 0) { $query->whereNotIn($key_name, $excluded_ids); } $query->delete(); }
/** * @param $joinTableAlias * @param $currentTableAlias * @param BelongsTo|Relation $relation * @param string $columnsPrefix */ protected function addJoinToQuery($joinTableAlias, $currentTableAlias, Relation $relation, $columnsPrefix = '') { $joinTableName = $relation->getRelated()->getTable(); $joinTable = implode(' as ', [$joinTableName, $joinTableAlias]); $joinLeftCondition = implode('.', [$joinTableAlias, $relation->getOtherKey()]); $joinRightCondition = implode('.', [$currentTableAlias, $relation->getForeignKey()]); $this->query->leftJoin($joinTable, $joinLeftCondition, '=', $joinRightCondition); $columns = $this->getColumns($joinTableName); $prefix = static::$prefix . $columnsPrefix . $joinTableAlias . '---'; foreach ($columns as $column) { $this->selectFromQuery($joinTableAlias, $column, $prefix . $column); } }
public function __construct(array $config, ModelManager $modelManager, Model $model, EloquentRelation $eloquentRelation, FieldFactory $fieldFactory) { $this->checkNameConfig($config); $this->name = $config['name']; $this->slug = $config['name']; $this->relatedModel = $model; $this->eloquentRelation = $eloquentRelation; $this->fieldFactory = $fieldFactory; $this->modelManager = $modelManager; $this->config = $config; $this->setup(); $this->modelAbstractor = \App::make('Anavel\\Crud\\Contracts\\Abstractor\\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $this->config); }
/** * DescendantsRelation constructor. * * @param QueryBuilder $builder * @param Model $model */ public function __construct(QueryBuilder $builder, Model $model) { if (!NestedSet::isNode($model)) { throw new InvalidArgumentException('Model must be node.'); } parent::__construct($builder, $model); }
/** * Boot the application events. * * @return void */ public function boot() { $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); Relation::morphMap(['paper_doc' => \Modules\Papers\Entities\PaperDoc::class, 'paper_semster_doc' => \Modules\Papers\Entities\PaperSemesterDoc::class]); }
/** * Create a new has many relationship instance. * * @param Illuminate\Database\Eloquent\Builder $query * @param Illuminate\Database\Eloquent\Model $parent * @param string $table * @param string $foreignKey * @param string $otherKey * @return void */ public function __construct(Builder $query, Model $parent, $table, $foreignKey, $otherKey) { $this->table = $table; $this->otherKey = $otherKey; $this->foreignKey = $foreignKey; parent::__construct($query, $parent); }
/** * Boot the application events. * * @return void */ public function boot() { $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); Relation::morphMap(['order_quran_excuses' => \Modules\Orders\Entities\OrderQuranExcuse::class]); }
/** * Add the constraints for a relationship count query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parent * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationCountQuery(Builder $query, Builder $parent) { if ($parent->getQuery()->from == $query->getQuery()->from) { return $this->getRelationCountQueryForSelfRelation($query, $parent); } return parent::getRelationCountQuery($query, $parent); }
/** * Boot the application events. * * @return void */ public function boot() { $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); Relation::morphMap(['teachers' => \Modules\Teachers\Entities\Teacher::class]); }
/** * Create a new has many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $firstKey * @param string $secondKey * @return void */ public function __construct(Builder $query, Model $farParent, Model $parent, $firstKey, $secondKey) { $this->firstKey = $firstKey; $this->secondKey = $secondKey; $this->farParent = $farParent; parent::__construct($query, $parent); }
/** * Add the constraints for a relationship query. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Builder $parent * @param array|mixed $columns * @return \Illuminate\Database\Eloquent\Builder */ public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*']) { if ($parent->getQuery()->from == $query->getQuery()->from) { return $this->getRelationQueryForSelfRelation($query, $parent, $columns); } return parent::getRelationQuery($query, $parent, $columns); }
/** * Boot the application events. * * @return void */ public function boot() { $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); Relation::morphMap([]); }
/** * Create a new relation instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $farChild * @param \Illuminate\Database\Eloquent\Model $parent * @param string $firstKey * @param string $localKey */ function __construct(Builder $query, Model $farChild, Model $parent, $firstKey, $localKey) { $this->farChild = $farChild; $this->firstKey = $firstKey; $this->localKey = $localKey; parent::__construct($query, $parent); }
/** * Create a new belongs to relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $foreignKey * @param string $otherKey * @param string $relation * @return void */ public function __construct(Builder $query, Model $parent, $foreignKey, $otherKey, $relation) { $this->otherKey = $otherKey; $this->relation = $relation; $this->foreignKey = $foreignKey; parent::__construct($query, $parent); }
/** * Create a new instance of relation. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param array $models * @param string|null $localKey * @param string $prefix * @param array $foreignKeyLookup */ public function __construct(Builder $query, Model $parent, array $models, $localKey = null, $prefix = '', $foreignKeyLookup = []) { $this->models = $models; $this->localKey = $localKey ?: $parent->getKeyName(); $this->prefix = $prefix; $this->foreignKeyLookup = $foreignKeyLookup; parent::__construct($query, $parent); }
public function __construct(Builder $builder, DatabaseNode $parent, Repository $repo) { parent::__construct($builder, $parent); $this->repository = $repo; $this->localKey = $parent->getIdName(); $this->foreignKey = $parent->getParentIdName(); $this->rootKey = $parent->getRootIdName(); }
/** * Sort by issues tag group * Note: this sort will return the collection * * @param Eloquent\Relations\Relation $query * @param string $tagGroup * @param string $order * * @return Eloquent\Collection */ public function sortByTag(Eloquent\Relations\Relation $query, $tagGroup, $order = 'asc') { // If tag group is string prefixed with tag: if (!is_numeric($tagGroup)) { $tagGroup = substr($tagGroup, strlen('tag:')); } $results = $query->get()->sort(function (Project\Issue $issue1, Project\Issue $issue2) use($tagGroup, $order) { $tag1 = $issue1->tags->where('parent.id', $tagGroup, false)->first(); $tag2 = $issue2->tags->where('parent.id', $tagGroup, false)->first(); $tag1 = $tag1 ? $tag1->name : ''; $tag2 = $tag2 ? $tag2->name : ''; if ($order === 'asc') { return strcmp($tag1, $tag2); } return strcmp($tag2, $tag1); }); return $results; }
/** * Boot the application events. * * @return void */ public function boot() { $this->registerTranslations(); $this->registerConfig(); $this->registerViews(); StudentGrade::creating(function ($grade) { $grade->semester_id = semester()->id; }); Relation::morphMap(['students' => \Modules\Students\Entities\Student::class]); }
/** * Register the User module service provider. * * @return void */ public function register() { // This service provider is a convenient place to register your modules // services in the IoC container. If you wish, you may make additional // methods or service providers to keep the code more focused and granular. App::register('App\\Modules\\User\\Providers\\RouteServiceProvider'); App::register('App\\Modules\\User\\Providers\\RepositoryServiceProvider'); Relation::morphMap([config('user_module.user_types.administrator') => App\Modules\User\Models\Administrator::class]); $this->registerNamespaces(); }
/** * Register any package services. * * @return void */ public function register() { $configPath = realpath(__DIR__ . '/../config/loggableModels.php'); $this->publishes([$configPath => $this->getConfigPath()], 'config'); $this->publishes([realpath(__DIR__ . '/../database/migrations/') => database_path('migrations')], 'migrations'); // Define Custom Polymorphic Types Relation::morphMap([]); $this->mergeConfigFrom($configPath, 'loggableModels'); $this->app->bind('loggable-models', function () { return new ModelLogHandler(); }); }
/** * Adds a join clause to the current relatedQuery using the relation Object. * * @param \Illuminate\Database\Query $query * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @return \Illuminate\Database\Querys */ protected function joinRelated($query, $relation) { $parentTable = $relation->getParent()->getTable(); $parentKey = $relation->getParent()->getKeyName(); $relatedTable = $relation->getRelated()->getTable(); $fk = $relation->getForeignKey(); $relationType = str_replace('Illuminate\\Database\\Eloquent\\Relations\\', '', get_class($relation)); switch ($relationType) { case 'BelongsTo': $query->join($relatedTable, "{$relatedTable}.{$parentKey}", '=', "{$parentTable}.{$fk}"); break; case 'HasOne': case 'HasMany': case 'MorphOne': case 'MorphMany': $query->join($relatedTable, "{$parentTable}.{$parentKey}", '=', "{$fk}"); break; case 'BelongsToMany': $table = $relation->getTable(); $otherKey = $relation->getOtherKey(); $query->join($table, "{$parentTable}.{$parentKey}", '=', "{$fk}")->join($relatedTable, "{$relatedTable}.{$parentKey}", '=', "{$otherKey}"); break; default: break; } return $query; }
/** * Sets up the existing relationship wheres. * * @param \Illuminate\Database\Eloquent\Relations\Relation $relationship * @param string $tableAlias * @param string $pivotAlias * @param string $pivot * * @return string */ public function getRelationshipWheres($relationship, $tableAlias, $pivotAlias = null, $pivot = null) { //get the relationship model $relationshipModel = $relationship->getRelated(); //get the query instance $query = $relationship->getQuery()->getQuery(); //get the connection instance $connection = $query->getConnection(); //one element of the relationship query's wheres is always useless (it will say pivot_table.other_id is null) //depending on whether or not softdeletes are enabled on the other model, this will be in either position 0 //or 1 of the wheres array array_splice($query->wheres, method_exists($relationshipModel, 'getDeletedAtColumn') ? 1 : 0, 1); //iterate over the wheres to properly alias the columns foreach ($query->wheres as &$where) { //alias the where columns $where['column'] = $this->aliasRelationshipWhere($where['column'], $tableAlias, $pivotAlias, $pivot); } $sql = $query->toSql(); $fullQuery = $this->interpolateQuery($sql, $connection->prepareBindings($query->getBindings())); $split = explode(' where ', $fullQuery); return isset($split[1]) ? $split[1] : ''; }
/** * Add the conversion that are defined on the related model of * the given media. * * @param \Spatie\MediaLibrary\Media $media */ protected function addConversionsFromRelatedModel(Media $media) { $modelName = Arr::get(Relation::morphMap(), $media->model_type, $media->model_type); /* * To prevent an sql query create a new model instead * of the using the associated one */ $model = new $modelName(); if ($model instanceof HasMediaConversions) { $model->registerMediaConversions(); } $this->items = $model->mediaConversions; }
public function __construct(array $config, ModelManager $modelManager, Model $model, EloquentRelation $eloquentRelation, FieldFactoryContract $fieldFactory) { $this->checkNameConfig($config); $this->name = $config['name']; $this->slug = $config['name']; $this->relatedModel = $model; $this->eloquentRelation = $eloquentRelation; $this->fieldFactory = $fieldFactory; $this->modelManager = $modelManager; $this->config = $config; $this->setup(); $relatedModelClassName = get_class($this->eloquentRelation->getRelated()); $relatedmodelRelationsConfig = []; foreach (config('anavel-crud.models') as $modelConfig) { if (is_array($modelConfig) && array_key_exists('model', $modelConfig) && $relatedModelClassName == $modelConfig['model']) { if (array_key_exists('relations', $modelConfig)) { $relatedmodelRelationsConfig['relations'] = $modelConfig['relations']; } } } $this->modelAbstractor = \App::make('Anavel\\Crud\\Contracts\\Abstractor\\ModelFactory')->getByClassName(get_class($this->eloquentRelation->getRelated()), $relatedmodelRelationsConfig); }
/** * Bootstrap the application services. * * @return void */ public function boot(GateContract $gate, Router $router) { // set view paths $this->loadViewsFrom(resource_path('ohio/content/views'), 'ohio-content'); // set backup view paths $this->loadViewsFrom(__DIR__ . '/../../resources/views', 'ohio-content'); // policies $this->registerPolicies($gate); // morphMap Relation::morphMap(['content/page' => Content\Page\Page::class]); // commands $this->commands(Content\Base\Commands\PublishCommand::class); Validator::extend('unique_route', \Ohio\Content\Base\Validators\RouteValidator::class . '@routeIsUnique'); }
/** * Get the relation instance for the given relation name. * * @param string $relation * * @return Relation * @throws BadRequestException */ public function getRelation($relation) { $query = Relation::noConstraints(function () use($relation) { /** @var BaseModel $model */ $model = $this->getModel(); $relationType = $model->getReferencingType($relation); if (RelationSchema::HAS_MANY === $relationType) { return $model->getHasManyByRelationName($relation); } elseif (RelationSchema::MANY_MANY === $relationType) { return $model->getBelongsToManyByRelationName($relation); } elseif (RelationSchema::BELONGS_TO === $relationType) { return $model->getBelongsToByRelationName($relation); } return null; }); if (!empty($query)) { return $query; } if (!method_exists($this->getModel(), $relation)) { throw new BadRequestException('Unknown relationship: ' . $relation); } return parent::getRelation($relation); }