code
stringlengths 31
1.39M
| docstring
stringlengths 23
16.8k
| func_name
stringlengths 1
126
| language
stringclasses 1
value | repo
stringlengths 7
63
| path
stringlengths 7
166
| url
stringlengths 50
220
| license
stringclasses 7
values |
---|---|---|---|---|---|---|---|
public function withMin($relation, $column)
{
return $this->withAggregate($relation, $column, 'min');
}
|
Add subselect queries to include the min of the relation's column.
@param string|array $relation
@param \Illuminate\Contracts\Database\Query\Expression|string $column
@return $this
|
withMin
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
public function withSum($relation, $column)
{
return $this->withAggregate($relation, $column, 'sum');
}
|
Add subselect queries to include the sum of the relation's column.
@param string|array $relation
@param \Illuminate\Contracts\Database\Query\Expression|string $column
@return $this
|
withSum
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
public function withAvg($relation, $column)
{
return $this->withAggregate($relation, $column, 'avg');
}
|
Add subselect queries to include the average of the relation's column.
@param string|array $relation
@param \Illuminate\Contracts\Database\Query\Expression|string $column
@return $this
|
withAvg
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
public function withExists($relation)
{
return $this->withAggregate($relation, '*', 'exists');
}
|
Add subselect queries to include the existence of related models.
@param string|array $relation
@return $this
|
withExists
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
protected function addHasWhere(Builder $hasQuery, Relation $relation, $operator, $count, $boolean)
{
$hasQuery->mergeConstraintsFrom($relation->getQuery());
return $this->canUseExistsForExistenceCheck($operator, $count)
? $this->addWhereExistsQuery($hasQuery->toBase(), $boolean, $operator === '<' && $count === 1)
: $this->addWhereCountQuery($hasQuery->toBase(), $operator, $count, $boolean);
}
|
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 $this
|
addHasWhere
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
public function mergeConstraintsFrom(Builder $from)
{
$whereBindings = $from->getQuery()->getRawBindings()['where'] ?? [];
$wheres = $from->getQuery()->from !== $this->getQuery()->from
? $this->requalifyWhereTables(
$from->getQuery()->wheres,
$from->getQuery()->grammar->getValue($from->getQuery()->from),
$this->getModel()->getTable()
) : $from->getQuery()->wheres;
// Here we have some other query that we want to merge the where constraints from. We will
// copy over any where constraints on the query as well as remove any global scopes the
// query might have removed. Then we will return ourselves with the finished merging.
return $this->withoutGlobalScopes(
$from->removedScopes()
)->mergeWheres(
$wheres, $whereBindings
);
}
|
Merge the where constraints from another query to the current query.
@param \Illuminate\Database\Eloquent\Builder<*> $from
@return $this
|
mergeConstraintsFrom
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
protected function requalifyWhereTables(array $wheres, string $from, string $to): array
{
return (new BaseCollection($wheres))->map(function ($where) use ($from, $to) {
return (new BaseCollection($where))->map(function ($value) use ($from, $to) {
return is_string($value) && str_starts_with($value, $from.'.')
? $to.'.'.Str::afterLast($value, '.')
: $value;
});
})->toArray();
}
|
Updates the table name for any columns with a new qualified name.
@param array $wheres
@param string $from
@param string $to
@return array
|
requalifyWhereTables
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
protected function addWhereCountQuery(QueryBuilder $query, $operator = '>=', $count = 1, $boolean = 'and')
{
$this->query->addBinding($query->getBindings(), 'where');
return $this->where(
new Expression('('.$query->toSql().')'),
$operator,
is_numeric($count) ? new Expression($count) : $count,
$boolean
);
}
|
Add a sub-query count clause to this query.
@param \Illuminate\Database\Query\Builder $query
@param string $operator
@param int $count
@param string $boolean
@return $this
|
addWhereCountQuery
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
protected function getRelationWithoutConstraints($relation)
{
return Relation::noConstraints(function () use ($relation) {
return $this->getModel()->{$relation}();
});
}
|
Get the "has relation" base query instance.
@param string $relation
@return \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>
|
getRelationWithoutConstraints
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
protected function canUseExistsForExistenceCheck($operator, $count)
{
return ($operator === '>=' || $operator === '<') && $count === 1;
}
|
Check if we can run an "exists" query to optimize performance.
@param string $operator
@param int $count
@return bool
|
canUseExistsForExistenceCheck
|
php
|
illuminate/database
|
Eloquent/Concerns/QueriesRelationships.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/QueriesRelationships.php
|
MIT
|
public function toResource(?string $resourceClass = null): JsonResource
{
if ($resourceClass === null) {
return $this->guessResource();
}
return $resourceClass::make($this);
}
|
Create a new resource object for the given resource.
@param class-string<\Illuminate\Http\Resources\Json\JsonResource>|null $resourceClass
@return \Illuminate\Http\Resources\Json\JsonResource
@throws \Throwable
|
toResource
|
php
|
illuminate/database
|
Eloquent/Concerns/TransformsToResource.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/TransformsToResource.php
|
MIT
|
protected function guessResource(): JsonResource
{
foreach (static::guessResourceName() as $resourceClass) {
if (is_string($resourceClass) && class_exists($resourceClass)) {
return $resourceClass::make($this);
}
}
throw new LogicException(sprintf('Failed to find resource class for model [%s].', get_class($this)));
}
|
Guess the resource class for the model.
@return \Illuminate\Http\Resources\Json\JsonResource
@throws \Throwable
|
guessResource
|
php
|
illuminate/database
|
Eloquent/Concerns/TransformsToResource.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/TransformsToResource.php
|
MIT
|
public static function guessResourceName(): array
{
$modelClass = static::class;
if (! Str::contains($modelClass, '\\Models\\')) {
return [];
}
$relativeNamespace = Str::after($modelClass, '\\Models\\');
$relativeNamespace = Str::contains($relativeNamespace, '\\')
? Str::before($relativeNamespace, '\\'.class_basename($modelClass))
: '';
$potentialResource = sprintf(
'%s\\Http\\Resources\\%s%s',
Str::before($modelClass, '\\Models'),
strlen($relativeNamespace) > 0 ? $relativeNamespace.'\\' : '',
class_basename($modelClass)
);
return [$potentialResource.'Resource', $potentialResource];
}
|
Guess the resource class name for the model.
@return array<class-string<\Illuminate\Http\Resources\Json\JsonResource>>
|
guessResourceName
|
php
|
illuminate/database
|
Eloquent/Concerns/TransformsToResource.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Concerns/TransformsToResource.php
|
MIT
|
public function __construct($factory, $pivot, $relationship)
{
$this->factory = $factory;
$this->pivot = $pivot;
$this->relationship = $relationship;
}
|
Create a new attached relationship definition.
@param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $factory
@param callable|array $pivot
@param string $relationship
|
__construct
|
php
|
illuminate/database
|
Eloquent/Factories/BelongsToManyRelationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/BelongsToManyRelationship.php
|
MIT
|
public function createFor(Model $model)
{
$factoryInstance = $this->factory instanceof Factory;
if ($factoryInstance) {
$relationship = $model->{$this->relationship}();
}
Collection::wrap($factoryInstance ? $this->factory->prependState($relationship->getQuery()->pendingAttributes)->create([], $model) : $this->factory)->each(function ($attachable) use ($model) {
$model->{$this->relationship}()->attach(
$attachable,
is_callable($this->pivot) ? call_user_func($this->pivot, $model) : $this->pivot
);
});
}
|
Create the attached relationship for the given model.
@param \Illuminate\Database\Eloquent\Model $model
@return void
|
createFor
|
php
|
illuminate/database
|
Eloquent/Factories/BelongsToManyRelationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/BelongsToManyRelationship.php
|
MIT
|
public function recycle($recycle)
{
if ($this->factory instanceof Factory) {
$this->factory = $this->factory->recycle($recycle);
}
return $this;
}
|
Specify the model instances to always use when creating relationships.
@param \Illuminate\Support\Collection $recycle
@return $this
|
recycle
|
php
|
illuminate/database
|
Eloquent/Factories/BelongsToManyRelationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/BelongsToManyRelationship.php
|
MIT
|
public function __construct($factory, $relationship)
{
$this->factory = $factory;
$this->relationship = $relationship;
}
|
Create a new "belongs to" relationship definition.
@param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Database\Eloquent\Model $factory
@param string $relationship
|
__construct
|
php
|
illuminate/database
|
Eloquent/Factories/BelongsToRelationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/BelongsToRelationship.php
|
MIT
|
public function attributesFor(Model $model)
{
$relationship = $model->{$this->relationship}();
return $relationship instanceof MorphTo ? [
$relationship->getMorphType() => $this->factory instanceof Factory ? $this->factory->newModel()->getMorphClass() : $this->factory->getMorphClass(),
$relationship->getForeignKeyName() => $this->resolver($relationship->getOwnerKeyName()),
] : [
$relationship->getForeignKeyName() => $this->resolver($relationship->getOwnerKeyName()),
];
}
|
Get the parent model attributes and resolvers for the given child model.
@param \Illuminate\Database\Eloquent\Model $model
@return array
|
attributesFor
|
php
|
illuminate/database
|
Eloquent/Factories/BelongsToRelationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/BelongsToRelationship.php
|
MIT
|
protected function resolver($key)
{
return function () use ($key) {
if (! $this->resolved) {
$instance = $this->factory instanceof Factory
? ($this->factory->getRandomRecycledModel($this->factory->modelName()) ?? $this->factory->create())
: $this->factory;
return $this->resolved = $key ? $instance->{$key} : $instance->getKey();
}
return $this->resolved;
};
}
|
Get the deferred resolver for this relationship's parent ID.
@param string|null $key
@return \Closure
|
resolver
|
php
|
illuminate/database
|
Eloquent/Factories/BelongsToRelationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/BelongsToRelationship.php
|
MIT
|
public function recycle($recycle)
{
if ($this->factory instanceof Factory) {
$this->factory = $this->factory->recycle($recycle);
}
return $this;
}
|
Specify the model instances to always use when creating relationships.
@param \Illuminate\Support\Collection $recycle
@return $this
|
recycle
|
php
|
illuminate/database
|
Eloquent/Factories/BelongsToRelationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/BelongsToRelationship.php
|
MIT
|
public function __construct(...$sequences)
{
$crossJoined = array_map(
function ($a) {
return array_merge(...$a);
},
Arr::crossJoin(...$sequences),
);
parent::__construct(...$crossJoined);
}
|
Create a new cross join sequence instance.
@param array ...$sequences
|
__construct
|
php
|
illuminate/database
|
Eloquent/Factories/CrossJoinSequence.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/CrossJoinSequence.php
|
MIT
|
public function __construct(
$count = null,
?Collection $states = null,
?Collection $has = null,
?Collection $for = null,
?Collection $afterMaking = null,
?Collection $afterCreating = null,
$connection = null,
?Collection $recycle = null,
bool $expandRelationships = true
) {
$this->count = $count;
$this->states = $states ?? new Collection;
$this->has = $has ?? new Collection;
$this->for = $for ?? new Collection;
$this->afterMaking = $afterMaking ?? new Collection;
$this->afterCreating = $afterCreating ?? new Collection;
$this->connection = $connection;
$this->recycle = $recycle ?? new Collection;
$this->faker = $this->withFaker();
$this->expandRelationships = $expandRelationships;
}
|
Create a new factory instance.
@param int|null $count
@param \Illuminate\Support\Collection|null $states
@param \Illuminate\Support\Collection|null $has
@param \Illuminate\Support\Collection|null $for
@param \Illuminate\Support\Collection|null $afterMaking
@param \Illuminate\Support\Collection|null $afterCreating
@param string|null $connection
@param \Illuminate\Support\Collection|null $recycle
@param bool $expandRelationships
|
__construct
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function new($attributes = [])
{
return (new static)->state($attributes)->configure();
}
|
Get a new factory instance for the given attributes.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@return static
|
new
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function times(int $count)
{
return static::new()->count($count);
}
|
Get a new factory instance for the given number of models.
@param int $count
@return static
|
times
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function raw($attributes = [], ?Model $parent = null)
{
if ($this->count === null) {
return $this->state($attributes)->getExpandedAttributes($parent);
}
return array_map(function () use ($attributes, $parent) {
return $this->state($attributes)->getExpandedAttributes($parent);
}, range(1, $this->count));
}
|
Get the raw attributes generated by the factory.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@param \Illuminate\Database\Eloquent\Model|null $parent
@return array<int|string, mixed>
|
raw
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function createOne($attributes = [])
{
return $this->count(null)->create($attributes);
}
|
Create a single model and persist it to the database.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@return TModel
|
createOne
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function createOneQuietly($attributes = [])
{
return $this->count(null)->createQuietly($attributes);
}
|
Create a single model and persist it to the database without dispatching any model events.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@return TModel
|
createOneQuietly
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function createMany(int|iterable|null $records = null)
{
$records ??= ($this->count ?? 1);
$this->count = null;
if (is_numeric($records)) {
$records = array_fill(0, $records, []);
}
return new EloquentCollection(
(new Collection($records))->map(function ($record) {
return $this->state($record)->create();
})
);
}
|
Create a collection of models and persist them to the database.
@param int|null|iterable<int, array<string, mixed>> $records
@return \Illuminate\Database\Eloquent\Collection<int, TModel>
|
createMany
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function createManyQuietly(int|iterable|null $records = null)
{
return Model::withoutEvents(fn () => $this->createMany($records));
}
|
Create a collection of models and persist them to the database without dispatching any model events.
@param int|null|iterable<int, array<string, mixed>> $records
@return \Illuminate\Database\Eloquent\Collection<int, TModel>
|
createManyQuietly
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function create($attributes = [], ?Model $parent = null)
{
if (! empty($attributes)) {
return $this->state($attributes)->create([], $parent);
}
$results = $this->make($attributes, $parent);
if ($results instanceof Model) {
$this->store(new Collection([$results]));
$this->callAfterCreating(new Collection([$results]), $parent);
} else {
$this->store($results);
$this->callAfterCreating($results, $parent);
}
return $results;
}
|
Create a collection of models and persist them to the database.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@param \Illuminate\Database\Eloquent\Model|null $parent
@return \Illuminate\Database\Eloquent\Collection<int, TModel>|TModel
|
create
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function createQuietly($attributes = [], ?Model $parent = null)
{
return Model::withoutEvents(fn () => $this->create($attributes, $parent));
}
|
Create a collection of models and persist them to the database without dispatching any model events.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@param \Illuminate\Database\Eloquent\Model|null $parent
@return \Illuminate\Database\Eloquent\Collection<int, TModel>|TModel
|
createQuietly
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function lazy(array $attributes = [], ?Model $parent = null)
{
return fn () => $this->create($attributes, $parent);
}
|
Create a callback that persists a model in the database when invoked.
@param array<string, mixed> $attributes
@param \Illuminate\Database\Eloquent\Model|null $parent
@return \Closure(): (\Illuminate\Database\Eloquent\Collection<int, TModel>|TModel)
|
lazy
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function store(Collection $results)
{
$results->each(function ($model) {
if (! isset($this->connection)) {
$model->setConnection($model->newQueryWithoutScopes()->getConnection()->getName());
}
$model->save();
foreach ($model->getRelations() as $name => $items) {
if ($items instanceof Enumerable && $items->isEmpty()) {
$model->unsetRelation($name);
}
}
$this->createChildren($model);
});
}
|
Set the connection name on the results and store them.
@param \Illuminate\Support\Collection<int, \Illuminate\Database\Eloquent\Model> $results
@return void
|
store
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function createChildren(Model $model)
{
Model::unguarded(function () use ($model) {
$this->has->each(function ($has) use ($model) {
$has->recycle($this->recycle)->createFor($model);
});
});
}
|
Create the children for the given model.
@param \Illuminate\Database\Eloquent\Model $model
@return void
|
createChildren
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function makeOne($attributes = [])
{
return $this->count(null)->make($attributes);
}
|
Make a single instance of the model.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@return TModel
|
makeOne
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function make($attributes = [], ?Model $parent = null)
{
if (! empty($attributes)) {
return $this->state($attributes)->make([], $parent);
}
if ($this->count === null) {
return tap($this->makeInstance($parent), function ($instance) {
$this->callAfterMaking(new Collection([$instance]));
});
}
if ($this->count < 1) {
return $this->newModel()->newCollection();
}
$instances = $this->newModel()->newCollection(array_map(function () use ($parent) {
return $this->makeInstance($parent);
}, range(1, $this->count)));
$this->callAfterMaking($instances);
return $instances;
}
|
Create a collection of models.
@param (callable(array<string, mixed>): array<string, mixed>)|array<string, mixed> $attributes
@param \Illuminate\Database\Eloquent\Model|null $parent
@return \Illuminate\Database\Eloquent\Collection<int, TModel>|TModel
|
make
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function makeInstance(?Model $parent)
{
return Model::unguarded(function () use ($parent) {
return tap($this->newModel($this->getExpandedAttributes($parent)), function ($instance) {
if (isset($this->connection)) {
$instance->setConnection($this->connection);
}
});
});
}
|
Make an instance of the model with the given attributes.
@param \Illuminate\Database\Eloquent\Model|null $parent
@return \Illuminate\Database\Eloquent\Model
|
makeInstance
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function getExpandedAttributes(?Model $parent)
{
return $this->expandAttributes($this->getRawAttributes($parent));
}
|
Get a raw attributes array for the model.
@param \Illuminate\Database\Eloquent\Model|null $parent
@return mixed
|
getExpandedAttributes
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function getRawAttributes(?Model $parent)
{
return $this->states->pipe(function ($states) {
return $this->for->isEmpty() ? $states : new Collection(array_merge([function () {
return $this->parentResolvers();
}], $states->all()));
})->reduce(function ($carry, $state) use ($parent) {
if ($state instanceof Closure) {
$state = $state->bindTo($this);
}
return array_merge($carry, $state($carry, $parent));
}, $this->definition());
}
|
Get the raw attributes for the model as an array.
@param \Illuminate\Database\Eloquent\Model|null $parent
@return array
|
getRawAttributes
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function parentResolvers()
{
return $this->for
->map(fn (BelongsToRelationship $for) => $for->recycle($this->recycle)->attributesFor($this->newModel()))
->collapse()
->all();
}
|
Create the parent relationship resolvers (as deferred Closures).
@return array
|
parentResolvers
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function expandAttributes(array $definition)
{
return (new Collection($definition))
->map($evaluateRelations = function ($attribute) {
if (! $this->expandRelationships && $attribute instanceof self) {
$attribute = null;
} elseif ($attribute instanceof self) {
$attribute = $this->getRandomRecycledModel($attribute->modelName())?->getKey()
?? $attribute->recycle($this->recycle)->create()->getKey();
} elseif ($attribute instanceof Model) {
$attribute = $attribute->getKey();
}
return $attribute;
})
->map(function ($attribute, $key) use (&$definition, $evaluateRelations) {
if (is_callable($attribute) && ! is_string($attribute) && ! is_array($attribute)) {
$attribute = $attribute($definition);
}
$attribute = $evaluateRelations($attribute);
$definition[$key] = $attribute;
return $attribute;
})
->all();
}
|
Expand all attributes to their underlying values.
@param array $definition
@return array
|
expandAttributes
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function state($state)
{
return $this->newInstance([
'states' => $this->states->concat([
is_callable($state) ? $state : fn () => $state,
]),
]);
}
|
Add a new state transformation to the model definition.
@param (callable(array<string, mixed>, Model|null): array<string, mixed>)|array<string, mixed> $state
@return static
|
state
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function prependState($state)
{
return $this->newInstance([
'states' => $this->states->prepend(
is_callable($state) ? $state : fn () => $state,
),
]);
}
|
Prepend a new state transformation to the model definition.
@param (callable(array<string, mixed>, Model|null): array<string, mixed>)|array<string, mixed> $state
@return static
|
prependState
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function set($key, $value)
{
return $this->state([$key => $value]);
}
|
Set a single model attribute.
@param string|int $key
@param mixed $value
@return static
|
set
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function sequence(...$sequence)
{
return $this->state(new Sequence(...$sequence));
}
|
Add a new sequenced state transformation to the model definition.
@param mixed ...$sequence
@return static
|
sequence
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function forEachSequence(...$sequence)
{
return $this->state(new Sequence(...$sequence))->count(count($sequence));
}
|
Add a new sequenced state transformation to the model definition and update the pending creation count to the size of the sequence.
@param array ...$sequence
@return static
|
forEachSequence
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function crossJoinSequence(...$sequence)
{
return $this->state(new CrossJoinSequence(...$sequence));
}
|
Add a new cross joined sequenced state transformation to the model definition.
@param array ...$sequence
@return static
|
crossJoinSequence
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function has(self $factory, $relationship = null)
{
return $this->newInstance([
'has' => $this->has->concat([new Relationship(
$factory, $relationship ?? $this->guessRelationship($factory->modelName())
)]),
]);
}
|
Define a child relationship for the model.
@param \Illuminate\Database\Eloquent\Factories\Factory $factory
@param string|null $relationship
@return static
|
has
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function guessRelationship(string $related)
{
$guess = Str::camel(Str::plural(class_basename($related)));
return method_exists($this->modelName(), $guess) ? $guess : Str::singular($guess);
}
|
Attempt to guess the relationship name for a "has" relationship.
@param string $related
@return string
|
guessRelationship
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function hasAttached($factory, $pivot = [], $relationship = null)
{
return $this->newInstance([
'has' => $this->has->concat([new BelongsToManyRelationship(
$factory,
$pivot,
$relationship ?? Str::camel(Str::plural(class_basename(
$factory instanceof Factory
? $factory->modelName()
: Collection::wrap($factory)->first()
)))
)]),
]);
}
|
Define an attached relationship for the model.
@param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $factory
@param (callable(): array<string, mixed>)|array<string, mixed> $pivot
@param string|null $relationship
@return static
|
hasAttached
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function for($factory, $relationship = null)
{
return $this->newInstance(['for' => $this->for->concat([new BelongsToRelationship(
$factory,
$relationship ?? Str::camel(class_basename(
$factory instanceof Factory ? $factory->modelName() : $factory
))
)])]);
}
|
Define a parent relationship for the model.
@param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Database\Eloquent\Model $factory
@param string|null $relationship
@return static
|
for
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function recycle($model)
{
// Group provided models by the type and merge them into existing recycle collection
return $this->newInstance([
'recycle' => $this->recycle
->flatten()
->merge(
Collection::wrap($model instanceof Model ? func_get_args() : $model)
->flatten()
)->groupBy(fn ($model) => get_class($model)),
]);
}
|
Provide model instances to use instead of any nested factory calls when creating relationships.
@param \Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection|array $model
@return static
|
recycle
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function getRandomRecycledModel($modelClassName)
{
return $this->recycle->get($modelClassName)?->random();
}
|
Retrieve a random model of a given type from previously provided models to recycle.
@template TClass of \Illuminate\Database\Eloquent\Model
@param class-string<TClass> $modelClassName
@return TClass|null
|
getRandomRecycledModel
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function afterMaking(Closure $callback)
{
return $this->newInstance(['afterMaking' => $this->afterMaking->concat([$callback])]);
}
|
Add a new "after making" callback to the model definition.
@param \Closure(TModel): mixed $callback
@return static
|
afterMaking
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function afterCreating(Closure $callback)
{
return $this->newInstance(['afterCreating' => $this->afterCreating->concat([$callback])]);
}
|
Add a new "after creating" callback to the model definition.
@param \Closure(TModel, \Illuminate\Database\Eloquent\Model|null): mixed $callback
@return static
|
afterCreating
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function callAfterMaking(Collection $instances)
{
$instances->each(function ($model) {
$this->afterMaking->each(function ($callback) use ($model) {
$callback($model);
});
});
}
|
Call the "after making" callbacks for the given model instances.
@param \Illuminate\Support\Collection $instances
@return void
|
callAfterMaking
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function callAfterCreating(Collection $instances, ?Model $parent = null)
{
$instances->each(function ($model) use ($parent) {
$this->afterCreating->each(function ($callback) use ($model, $parent) {
$callback($model, $parent);
});
});
}
|
Call the "after creating" callbacks for the given model instances.
@param \Illuminate\Support\Collection $instances
@param \Illuminate\Database\Eloquent\Model|null $parent
@return void
|
callAfterCreating
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function count(?int $count)
{
return $this->newInstance(['count' => $count]);
}
|
Specify how many models should be generated.
@param int|null $count
@return static
|
count
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function withoutParents()
{
return $this->newInstance(['expandRelationships' => false]);
}
|
Indicate that related parent models should not be created.
@return static
|
withoutParents
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function getConnectionName()
{
return $this->connection;
}
|
Get the name of the database connection that is used to generate models.
@return string
|
getConnectionName
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function connection(string $connection)
{
return $this->newInstance(['connection' => $connection]);
}
|
Specify the database connection that should be used to generate models.
@param string $connection
@return static
|
connection
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function newInstance(array $arguments = [])
{
return new static(...array_values(array_merge([
'count' => $this->count,
'states' => $this->states,
'has' => $this->has,
'for' => $this->for,
'afterMaking' => $this->afterMaking,
'afterCreating' => $this->afterCreating,
'connection' => $this->connection,
'recycle' => $this->recycle,
'expandRelationships' => $this->expandRelationships,
], $arguments)));
}
|
Create a new instance of the factory builder with the given mutated properties.
@param array $arguments
@return static
|
newInstance
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function newModel(array $attributes = [])
{
$model = $this->modelName();
return new $model($attributes);
}
|
Get a new model instance.
@param array<string, mixed> $attributes
@return TModel
|
newModel
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function modelName()
{
if ($this->model !== null) {
return $this->model;
}
$resolver = static::$modelNameResolvers[static::class] ?? static::$modelNameResolvers[self::class] ?? static::$modelNameResolver ?? function (self $factory) {
$namespacedFactoryBasename = Str::replaceLast(
'Factory', '', Str::replaceFirst(static::$namespace, '', $factory::class)
);
$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));
$appNamespace = static::appNamespace();
return class_exists($appNamespace.'Models\\'.$namespacedFactoryBasename)
? $appNamespace.'Models\\'.$namespacedFactoryBasename
: $appNamespace.$factoryBasename;
};
return $resolver($this);
}
|
Get the name of the model that is generated by the factory.
@return class-string<TModel>
|
modelName
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function guessModelNamesUsing(callable $callback)
{
static::$modelNameResolvers[static::class] = $callback;
}
|
Specify the callback that should be invoked to guess model names based on factory names.
@param callable(self): class-string<TModel> $callback
@return void
|
guessModelNamesUsing
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function useNamespace(string $namespace)
{
static::$namespace = $namespace;
}
|
Specify the default namespace that contains the application's model factories.
@param string $namespace
@return void
|
useNamespace
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function factoryForModel(string $modelName)
{
$factory = static::resolveFactoryName($modelName);
return $factory::new();
}
|
Get a new factory instance for the given model name.
@template TClass of \Illuminate\Database\Eloquent\Model
@param class-string<TClass> $modelName
@return \Illuminate\Database\Eloquent\Factories\Factory<TClass>
|
factoryForModel
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function guessFactoryNamesUsing(callable $callback)
{
static::$factoryNameResolver = $callback;
}
|
Specify the callback that should be invoked to guess factory names based on dynamic relationship names.
@param callable(class-string<\Illuminate\Database\Eloquent\Model>): class-string<\Illuminate\Database\Eloquent\Factories\Factory> $callback
@return void
|
guessFactoryNamesUsing
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected function withFaker()
{
return Container::getInstance()->make(Generator::class);
}
|
Get a new Faker instance.
@return \Faker\Generator
|
withFaker
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function resolveFactoryName(string $modelName)
{
$resolver = static::$factoryNameResolver ?? function (string $modelName) {
$appNamespace = static::appNamespace();
$modelName = Str::startsWith($modelName, $appNamespace.'Models\\')
? Str::after($modelName, $appNamespace.'Models\\')
: Str::after($modelName, $appNamespace);
return static::$namespace.$modelName.'Factory';
};
return $resolver($modelName);
}
|
Get the factory name for the given model name.
@template TClass of \Illuminate\Database\Eloquent\Model
@param class-string<TClass> $modelName
@return class-string<\Illuminate\Database\Eloquent\Factories\Factory<TClass>>
|
resolveFactoryName
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
protected static function appNamespace()
{
try {
return Container::getInstance()
->make(Application::class)
->getNamespace();
} catch (Throwable) {
return 'App\\';
}
}
|
Get the application namespace for the application.
@return string
|
appNamespace
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function flushState()
{
static::$modelNameResolver = null;
static::$modelNameResolvers = [];
static::$factoryNameResolver = null;
static::$namespace = 'Database\\Factories\\';
}
|
Flush the factory's global state.
@return void
|
flushState
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public function __call($method, $parameters)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
if ($method === 'trashed' && in_array(SoftDeletes::class, class_uses_recursive($this->modelName()))) {
return $this->state([
$this->newModel()->getDeletedAtColumn() => $parameters[0] ?? Carbon::now()->subDay(),
]);
}
if (! Str::startsWith($method, ['for', 'has'])) {
static::throwBadMethodCallException($method);
}
$relationship = Str::camel(Str::substr($method, 3));
$relatedModel = get_class($this->newModel()->{$relationship}()->getRelated());
if (method_exists($relatedModel, 'newFactory')) {
$factory = $relatedModel::newFactory() ?? static::factoryForModel($relatedModel);
} else {
$factory = static::factoryForModel($relatedModel);
}
if (str_starts_with($method, 'for')) {
return $this->for($factory->state($parameters[0] ?? []), $relationship);
} elseif (str_starts_with($method, 'has')) {
return $this->has(
$factory
->count(is_numeric($parameters[0] ?? null) ? $parameters[0] : 1)
->state((is_callable($parameters[0] ?? null) || is_array($parameters[0] ?? null)) ? $parameters[0] : ($parameters[1] ?? [])),
$relationship
);
}
}
|
Proxy dynamic factory methods onto their proper methods.
@param string $method
@param array $parameters
@return mixed
|
__call
|
php
|
illuminate/database
|
Eloquent/Factories/Factory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Factory.php
|
MIT
|
public static function factory($count = null, $state = [])
{
$factory = static::newFactory() ?? Factory::factoryForModel(static::class);
return $factory
->count(is_numeric($count) ? $count : null)
->state(is_callable($count) || is_array($count) ? $count : $state);
}
|
Get a new factory instance for the model.
@param (callable(array<string, mixed>, static|null): array<string, mixed>)|array<string, mixed>|int|null $count
@param (callable(array<string, mixed>, static|null): array<string, mixed>)|array<string, mixed> $state
@return TFactory
|
factory
|
php
|
illuminate/database
|
Eloquent/Factories/HasFactory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/HasFactory.php
|
MIT
|
protected static function newFactory()
{
if (isset(static::$factory)) {
return static::$factory::new();
}
return static::getUseFactoryAttribute() ?? null;
}
|
Create a new factory instance for the model.
@return TFactory|null
|
newFactory
|
php
|
illuminate/database
|
Eloquent/Factories/HasFactory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/HasFactory.php
|
MIT
|
protected static function getUseFactoryAttribute()
{
$attributes = (new \ReflectionClass(static::class))
->getAttributes(UseFactory::class);
if ($attributes !== []) {
$useFactory = $attributes[0]->newInstance();
$factory = $useFactory->factoryClass::new();
$factory->guessModelNamesUsing(fn () => static::class);
return $factory;
}
}
|
Get the factory from the UseFactory class attribute.
@return TFactory|null
|
getUseFactoryAttribute
|
php
|
illuminate/database
|
Eloquent/Factories/HasFactory.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/HasFactory.php
|
MIT
|
public function __construct(Factory $factory, $relationship)
{
$this->factory = $factory;
$this->relationship = $relationship;
}
|
Create a new child relationship instance.
@param \Illuminate\Database\Eloquent\Factories\Factory $factory
@param string $relationship
|
__construct
|
php
|
illuminate/database
|
Eloquent/Factories/Relationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Relationship.php
|
MIT
|
public function createFor(Model $parent)
{
$relationship = $parent->{$this->relationship}();
if ($relationship instanceof MorphOneOrMany) {
$this->factory->state([
$relationship->getMorphType() => $relationship->getMorphClass(),
$relationship->getForeignKeyName() => $relationship->getParentKey(),
])->prependState($relationship->getQuery()->pendingAttributes)->create([], $parent);
} elseif ($relationship instanceof HasOneOrMany) {
$this->factory->state([
$relationship->getForeignKeyName() => $relationship->getParentKey(),
])->prependState($relationship->getQuery()->pendingAttributes)->create([], $parent);
} elseif ($relationship instanceof BelongsToMany) {
$relationship->attach(
$this->factory->prependState($relationship->getQuery()->pendingAttributes)->create([], $parent)
);
}
}
|
Create the child relationship for the given parent model.
@param \Illuminate\Database\Eloquent\Model $parent
@return void
|
createFor
|
php
|
illuminate/database
|
Eloquent/Factories/Relationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Relationship.php
|
MIT
|
public function recycle($recycle)
{
$this->factory = $this->factory->recycle($recycle);
return $this;
}
|
Specify the model instances to always use when creating relationships.
@param \Illuminate\Support\Collection $recycle
@return $this
|
recycle
|
php
|
illuminate/database
|
Eloquent/Factories/Relationship.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Relationship.php
|
MIT
|
public function __construct(...$sequence)
{
$this->sequence = $sequence;
$this->count = count($sequence);
}
|
Create a new sequence instance.
@param mixed ...$sequence
|
__construct
|
php
|
illuminate/database
|
Eloquent/Factories/Sequence.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Sequence.php
|
MIT
|
public function count(): int
{
return $this->count;
}
|
Get the current count of the sequence items.
@return int
|
count
|
php
|
illuminate/database
|
Eloquent/Factories/Sequence.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Sequence.php
|
MIT
|
public function __invoke()
{
return tap(value($this->sequence[$this->index % $this->count], $this), function () {
$this->index = $this->index + 1;
});
}
|
Get the next value in the sequence.
@return mixed
|
__invoke
|
php
|
illuminate/database
|
Eloquent/Factories/Sequence.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Factories/Sequence.php
|
MIT
|
public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey, $relationName)
{
$this->ownerKey = $ownerKey;
$this->relationName = $relationName;
$this->foreignKey = $foreignKey;
// In the underlying base relationship class, this variable is referred to as
// the "parent" since most relationships are not inversed. But, since this
// one is we will create a "child" variable for much better readability.
$this->child = $child;
parent::__construct($query, $child);
}
|
Create a new belongs to relationship instance.
@param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
@param TDeclaringModel $child
@param string $foreignKey
@param string $ownerKey
@param string $relationName
|
__construct
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function addConstraints()
{
if (static::$constraints) {
// For belongs to relationships, which are essentially the inverse of has one
// or has many relationships, we need to actually query on the primary key
// of the related models matching on the foreign key that's on a parent.
$key = $this->getQualifiedOwnerKeyName();
$this->query->where($key, '=', $this->getForeignKeyFrom($this->child));
}
}
|
Set the base constraints on the relation query.
@return void
|
addConstraints
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
protected function getEagerModelKeys(array $models)
{
$keys = [];
// First we need to gather all of the keys from the parent models so we know what
// to query for via the eager loading query. We will add them to an array then
// execute a "where in" statement to gather up all of those related records.
foreach ($models as $model) {
if (! is_null($value = $this->getForeignKeyFrom($model))) {
$keys[] = $value;
}
}
sort($keys);
return array_values(array_unique($keys));
}
|
Gather the keys from an array of related models.
@param array<int, TDeclaringModel> $models
@return array
|
getEagerModelKeys
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function associate($model)
{
$ownerKey = $model instanceof Model ? $model->getAttribute($this->ownerKey) : $model;
$this->child->setAttribute($this->foreignKey, $ownerKey);
if ($model instanceof Model) {
$this->child->setRelation($this->relationName, $model);
} else {
$this->child->unsetRelation($this->relationName);
}
return $this->child;
}
|
Associate the model instance to the given parent.
@param TRelatedModel|int|string|null $model
@return TDeclaringModel
|
associate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function dissociate()
{
$this->child->setAttribute($this->foreignKey, null);
return $this->child->setRelation($this->relationName, null);
}
|
Dissociate previously associated model from the given parent.
@return TDeclaringModel
|
dissociate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function disassociate()
{
return $this->dissociate();
}
|
Alias of "dissociate" method.
@return TDeclaringModel
|
disassociate
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function touch()
{
if (! is_null($this->getParentKey())) {
parent::touch();
}
}
|
Touch all of the related models for the relationship.
@return void
|
touch
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function getRelationExistenceQueryForSelfRelation(Builder $query, Builder $parentQuery, $columns = ['*'])
{
$query->select($columns)->from(
$query->getModel()->getTable().' as '.$hash = $this->getRelationCountHash()
);
$query->getModel()->setTable($hash);
return $query->whereColumn(
$hash.'.'.$this->ownerKey, '=', $this->getQualifiedForeignKeyName()
);
}
|
Add the constraints for a relationship query on the same table.
@param \Illuminate\Database\Eloquent\Builder<TRelatedModel> $query
@param \Illuminate\Database\Eloquent\Builder<TDeclaringModel> $parentQuery
@param array|mixed $columns
@return \Illuminate\Database\Eloquent\Builder<TRelatedModel>
|
getRelationExistenceQueryForSelfRelation
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
protected function relationHasIncrementingId()
{
return $this->related->getIncrementing() &&
in_array($this->related->getKeyType(), ['int', 'integer']);
}
|
Determine if the related model has an auto-incrementing ID.
@return bool
|
relationHasIncrementingId
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
protected function newRelatedInstanceFor(Model $parent)
{
return $this->related->newInstance();
}
|
Make a new related instance for the given model.
@param TDeclaringModel $parent
@return TRelatedModel
|
newRelatedInstanceFor
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function getChild()
{
return $this->child;
}
|
Get the child of the relationship.
@return TDeclaringModel
|
getChild
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function getForeignKeyName()
{
return $this->foreignKey;
}
|
Get the foreign key of the relationship.
@return string
|
getForeignKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function getQualifiedForeignKeyName()
{
return $this->child->qualifyColumn($this->foreignKey);
}
|
Get the fully qualified foreign key of the relationship.
@return string
|
getQualifiedForeignKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function getParentKey()
{
return $this->getForeignKeyFrom($this->child);
}
|
Get the key value of the child's foreign key.
@return mixed
|
getParentKey
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function getOwnerKeyName()
{
return $this->ownerKey;
}
|
Get the associated key of the relationship.
@return string
|
getOwnerKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
public function getQualifiedOwnerKeyName()
{
return $this->related->qualifyColumn($this->ownerKey);
}
|
Get the fully qualified associated key of the relationship.
@return string
|
getQualifiedOwnerKeyName
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
protected function getRelatedKeyFrom(Model $model)
{
return $model->{$this->ownerKey};
}
|
Get the value of the model's foreign key.
@param TRelatedModel $model
@return int|string
|
getRelatedKeyFrom
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
protected function getForeignKeyFrom(Model $model)
{
$foreignKey = $model->{$this->foreignKey};
return enum_value($foreignKey);
}
|
Get the value of the model's foreign key.
@param TDeclaringModel $model
@return mixed
|
getForeignKeyFrom
|
php
|
illuminate/database
|
Eloquent/Relations/BelongsTo.php
|
https://github.com/illuminate/database/blob/master/Eloquent/Relations/BelongsTo.php
|
MIT
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.