Get the class name for polymorphic relations.
public getMorphClass ( ) : string | ||
Résultat | string |
/** * Create the pivot table records for assigning the role to given models. * * @param \Illuminate\Database\Eloquent\Model $model * @param array $keys * @return array */ protected function createAssignRecords(Model $model, array $keys) { $type = $model->getMorphClass(); return array_map(function ($key) use($type) { return ['role_id' => $this->getKey(), 'entity_type' => $type, 'entity_id' => $key]; }, $keys); }
/** * Create a new morph to many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $name * @param string $table * @param string $foreignKey * @param string $otherKey * @param string $relationName * @param bool $inverse * @return void */ public function __construct(Builder $query, Model $parent, $name, $table, $foreignKey, $otherKey, $relationName = null, $inverse = false) { $this->inverse = $inverse; $this->morphType = $name . '_type'; $this->morphClass = $inverse ? $query->getModel()->getMorphClass() : $parent->getMorphClass(); parent::__construct($query, $parent, $table, $foreignKey, $otherKey, $relationName); }
/** * Constrain a query to an ability for a specific model. * * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Eloquent\Model $model * @param bool $strict * @return void */ protected function constrainByModel($query, Model $model, $strict = false) { $query->where(function ($query) use($model, $strict) { $query->where($this->table . '.entity_type', $model->getMorphClass()); $query->where($this->abilitySubqueryConstraint($model, $strict)); }); }
/** * @param Model $pointable * * @return static */ public function getCurrentPoints(Model $pointable) { $currentPoint = Transaction::where('pointable_id', $pointable->id)->where('pointable_type', $pointable->getMorphClass())->orderBy('created_at', 'desc')->lists('current')->first(); if (!$currentPoint) { $currentPoint = 0.0; } return $currentPoint; }
/** * @param String $roles * @param Model $model * @return bool * @throws IsNotStringException */ public function hasRolesIn($roles, Model $model) { if (is_string($roles)) { $perm = $this->newPermission(); $permissions = $perm->where('permable_type', $model->getMorphClass())->where('permable_id', $model->id)->where('user_id', $this->id)->whereHas('roles', function ($query) use($roles) { $query->where('name', $roles); })->first(); return !is_null($permissions); } throw new IsNotStringException('Parameter 1 must be a string.'); }
/** * Constrain a query to an ability for a specific model. * * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder $query * @param \Illuminate\Database\Eloquent\Model $model * @return void */ public function scopeForModel($query, Model $model) { $query->where(function ($query) use($model) { $query->where('entity_type', $model->getMorphClass()); $query->where(function ($query) use($model) { $query->whereNull('entity_id'); if ($model->exists) { $query->orWhere('entity_id', $model->getKey()); } }); }); }
/** * Associate the model instance to the given parent. * * @param \Illuminate\Database\Eloquent\Model $model * @return \Illuminate\Database\Eloquent\Model */ public function associate(Model $model) { $this->parent->setAttribute($this->foreignKey, $model->getKey()); $this->parent->setAttribute($this->morphType, $model->getMorphClass()); return $this->parent->setRelation($this->relation, $model); }
/** * Join relation's table accordingly. * * @param \Sofa\Eloquence\Builder $query * @param string $segment * @param \Illuminate\Database\Eloquent\Model $parent * @return array */ protected function joinSegment(Builder $query, $segment, EloquentModel $parent) { $relation = $parent->{$segment}(); $related = $relation->getRelated(); $table = $related->getTable(); // If the table has been already joined let's skip it. Otherwise we will left join // it in order to allow using some query methods on mapped columns. Polymorphic // relations require also additional constraints, so let's handle it as well. if (!$this->alreadyJoined($query, $table)) { list($fk, $pk) = $this->getJoinKeys($relation); $query->leftJoin($table, function ($join) use($fk, $pk, $relation, $parent, $related) { $join->on($fk, '=', $pk); if ($relation instanceof MorphOne || $relation instanceof MorphTo) { $morphClass = $relation instanceof MorphOne ? $parent->getMorphClass() : $related->getMorphClass(); $join->where($relation->getMorphType(), '=', $morphClass); } }); } return [$table, $related]; }
/** * Create a new morph one or many relationship instance. * * @param \Illuminate\Database\Eloquent\Builder $query * @param \Illuminate\Database\Eloquent\Model $parent * @param string $type * @param string $id * @param string $localKey * @return void */ public function __construct(Builder $query, Model $parent, $type, $id, $localKey) { $this->morphType = $type; $this->morphClass = $parent->getMorphClass(); parent::__construct($query, $parent, $id, $localKey); }
/** * Create a new permission for a specific model. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $name * @return static */ public static function createForModel(Model $model, $name) { return static::forceCreate(['name' => $name, 'entity_type' => $model->getMorphClass(), 'entity_id' => $model->exists ? $model->getKey() : null]); }
/** * Get the join clause for related table. * * @param \Illuminate\Database\Eloquent\Model $parent * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param string $type * @param string $table * @return \Illuminate\Database\Query\JoinClause */ protected function getJoinClause(Model $parent, Relation $relation, $table, $type) { list($fk, $pk) = $this->getJoinKeys($relation); $join = (new Join($type, $table))->on($fk, '=', $pk); if ($relation instanceof MorphOneOrMany) { $join->where($relation->getMorphType(), '=', $parent->getMorphClass()); } return $join; }
/** * Add comment .. * * @param $comment * @param Model $author * @param string $status */ public function addComment($comment, Model $author, $status = 'pending') { $commentRow = new Comment(['author_id' => $author->id, 'author_type' => $author->getMorphClass(), 'comment' => $comment, 'status' => $status]); return $this->comments()->save($commentRow); }
/** * Get the cache key for the given model's cache type. * * @param \Illuminate\Database\Eloquent\Model $model * @param string $type * @param bool $allowed * @return string */ protected function getCacheKey(Model $model, $type, $allowed = true) { return implode('-', [$this->tag, $type, $model->getMorphClass(), $model->getKey(), $allowed ? 'a' : 'f']); }
/** * Makes a Mockery expectation for one item. * * @return \Mockery\Matcher\Closure */ protected function makeElasticSearchOneExpectation(Model $model) { return \Mockery::on(function (Model $entity) use($model) { return get_class($entity) == $model->getMorphClass() && $entity->getKey() == $model->getKey(); }); }
/** * @param $query * @param Model $model * @param string $groupSlug * @return \Illuminate\Database\Eloquent\Builder */ public function scopeWhereGroup($query, $model, $groupSlug) { $groupsPivotTable = config('friendships.tables.fr_groups_pivot'); $friendsPivotTable = config('friendships.tables.fr_pivot'); $groupsAvailable = config('friendships.groups', []); if ('' !== $groupSlug && isset($groupsAvailable[$groupSlug])) { $groupId = $groupsAvailable[$groupSlug]; $query->join($groupsPivotTable, function ($join) use($groupsPivotTable, $friendsPivotTable, $groupId, $model) { $join->on($groupsPivotTable . '.friendship_id', '=', $friendsPivotTable . '.id')->where($groupsPivotTable . '.group_id', '=', $groupId)->where(function ($query) use($groupsPivotTable, $friendsPivotTable, $model) { $query->where($groupsPivotTable . '.friend_id', '!=', $model->getKey())->where($groupsPivotTable . '.friend_type', '=', $model->getMorphClass()); })->orWhere($groupsPivotTable . '.friend_type', '!=', $model->getMorphClass()); }); } return $query; }
/** * Get a constraint for abilities that have been granted to the given authority. * * @param \Illuminate\Database\Eloquent\Model $authority * @param bool $allowed * @return \Closure */ protected function getAuthorityConstraint(Model $authority, $allowed) { return function ($query) use($authority, $allowed) { $permissions = Models::table('permissions'); $abilities = Models::table('abilities'); $table = $authority->getTable(); $prefix = Models::prefix(); $query->from($table)->join($permissions, $table . '.id', '=', $permissions . '.entity_id')->whereRaw("{$prefix}{$permissions}.ability_id = {$prefix}{$abilities}.id")->where("{$permissions}.entity_type", $authority->getMorphClass())->where("{$permissions}.forbidden", !$allowed)->where("{$table}.{$authority->getKeyName()}", $authority->getKey()); }; }