本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::getMorphClass方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::getMorphClass方法的具体用法?PHP Model::getMorphClass怎么用?PHP Model::getMorphClass使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::getMorphClass方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createAssignRecords
/**
* 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);
}
示例2: __construct
/**
* 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);
}
示例3: constrainByModel
/**
* 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));
});
}
示例4: getCurrentPoints
/**
* @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;
}
示例5: hasRolesIn
/**
* @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.');
}
示例6: scopeForModel
/**
* 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());
}
});
});
}
示例7: associate
/**
* 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);
}
示例8: joinSegment
/**
* 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];
}
示例9: __construct
/**
* 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);
}
示例10: createForModel
/**
* 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]);
}
示例11: getJoinClause
/**
* 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;
}
示例12: addComment
/**
* 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);
}
示例13: getCacheKey
/**
* 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']);
}
示例14: makeElasticSearchOneExpectation
/**
* 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();
});
}
示例15: scopeWhereGroup
/**
* @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;
}