Skip to content

Commit

Permalink
fix conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Feb 18, 2025
2 parents 313bd9d + c0dd42d commit d930357
Show file tree
Hide file tree
Showing 11 changed files with 132 additions and 33 deletions.
1 change: 0 additions & 1 deletion Concerns/BuildsQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
/**
* @template TValue
*
* @mixin \Illuminate\Database\Eloquent\Builder
* @mixin \Illuminate\Database\Query\Builder
*/
trait BuildsQueries
Expand Down
20 changes: 16 additions & 4 deletions Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,21 @@ public function find($id, $columns = ['*'])
return $this->whereKey($id)->first($columns);
}

/**
* Find a sole model by its primary key.
*
* @param mixed $id
* @param array|string $columns
* @return TModel
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TModel>
* @throws \Illuminate\Database\MultipleRecordsFoundException
*/
public function findSole($id, $columns = ['*'])
{
return $this->whereKey($id)->sole($columns);
}

/**
* Find multiple models by their primary keys.
*
Expand Down Expand Up @@ -1013,10 +1028,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',

$total = value($total) ?? $this->toBase()->getCountForPagination();

$perPage = ($perPage instanceof Closure
? $perPage($total)
: $perPage
) ?: $this->model->getPerPage();
$perPage = value($perPage, $total) ?? $this->model->getPerPage();

$results = $total
? $this->forPage($page, $perPage)->get($columns)
Expand Down
13 changes: 13 additions & 0 deletions Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,19 @@ public function contains($key, $operator = null, $value = null)
return parent::contains(fn ($model) => $model->getKey() == $key);
}

/**
* Determine if a key does not exist in the collection.
*
* @param (callable(TModel, TKey): bool)|TModel|string|int $key
* @param mixed $operator
* @param mixed $value
* @return bool
*/
public function doesntContain($key, $operator = null, $value = null)
{
return ! $this->contains(...func_get_args());
}

/**
* Get the array of primary keys.
*
Expand Down
8 changes: 6 additions & 2 deletions Eloquent/Concerns/HasTimestamps.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ public function getUpdatedAtColumn()
*/
public function getQualifiedCreatedAtColumn()
{
return $this->qualifyColumn($this->getCreatedAtColumn());
$column = $this->getCreatedAtColumn();

return $column ? $this->qualifyColumn($column) : null;
}

/**
Expand All @@ -171,7 +173,9 @@ public function getQualifiedCreatedAtColumn()
*/
public function getQualifiedUpdatedAtColumn()
{
return $this->qualifyColumn($this->getUpdatedAtColumn());
$column = $this->getUpdatedAtColumn();

return $column ? $this->qualifyColumn($column) : null;
}

/**
Expand Down
23 changes: 21 additions & 2 deletions Eloquent/Concerns/QueriesRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public function whereHas($relation, ?Closure $callback = null, $operator = '>=',
/**
* Add a relationship count / exists condition to the query with where clauses.
*
* Also load the relationship with same condition.
* Also load the relationship with the same condition.
*
* @param string $relation
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<*>|\Illuminate\Database\Eloquent\Relations\Relation<*, *, *>): mixed)|null $callback
Expand Down Expand Up @@ -374,7 +374,7 @@ public function whereHasMorph($relation, $types, ?Closure $callback = null, $ope
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
*
* @param \Illuminate\Database\Eloquent\Relations\MorphTo<TRelatedModel, *>|string $relation
* @param string|array<int, array> $types
* @param string|array<int, string> $types
* @param (\Closure(\Illuminate\Database\Eloquent\Builder<TRelatedModel>, string): mixed)|null $callback
* @param string $operator
* @param int $count
Expand Down Expand Up @@ -437,6 +437,25 @@ public function whereRelation($relation, $column, $operator = null, $value = nul
});
}

/**
* Add a basic where clause to a relationship query and eager-load the relationship with the same conditions.
*
* @param \Illuminate\Database\Eloquent\Relations\Relation<*, *, *>|string $relation
* @param \Closure|string|array|\Illuminate\Contracts\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @return $this
*/
public function withWhereRelation($relation, $column, $operator = null, $value = null)
{
return $this->whereRelation($relation, $column, $operator, $value)
->with([
$relation => fn ($query) => $column instanceof Closure
? $column($query)
: $query->where($column, $operator, $value),
]);
}

/**
* Add an "or where" clause to a relationship query.
*
Expand Down
24 changes: 18 additions & 6 deletions Eloquent/Factories/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,11 @@ abstract class Factory
public static $namespace = 'Database\\Factories\\';

/**
* The default model name resolver.
* The default model name resolvers.
*
* @var callable(self): class-string<TModel>
* @var array<class-string, callable(self): class-string<TModel>>
*/
protected static $modelNameResolver;
protected static $modelNameResolvers = [];

/**
* The factory name resolver.
Expand Down Expand Up @@ -810,9 +810,9 @@ public function modelName()
return $this->model;
}

$resolver = static::$modelNameResolver ?? function (self $factory) {
$resolver = static::$modelNameResolvers[static::class] ?? static::$modelNameResolvers[self::class] ?? function (self $factory) {
$namespacedFactoryBasename = Str::replaceLast(
'Factory', '', Str::replaceFirst(static::$namespace, '', get_class($factory))
'Factory', '', Str::replaceFirst(static::$namespace, '', $factory::class)
);

$factoryBasename = Str::replaceLast('Factory', '', class_basename($factory));
Expand All @@ -835,7 +835,7 @@ public function modelName()
*/
public static function guessModelNamesUsing(callable $callback)
{
static::$modelNameResolver = $callback;
static::$modelNameResolvers[static::class] = $callback;
}

/**
Expand Down Expand Up @@ -924,6 +924,18 @@ protected static function appNamespace()
}
}

/**
* Flush the factory's global state.
*
* @return void
*/
public static function flushState()
{
static::$modelNameResolvers = [];
static::$factoryNameResolver = null;
static::$namespace = 'Database\\Factories\\';
}

/**
* Proxy dynamic factory methods onto their proper methods.
*
Expand Down
34 changes: 19 additions & 15 deletions Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,9 @@ public function updateQuietly(array $attributes = [], array $options = [])
*/
protected function incrementQuietly($column, $amount = 1, array $extra = [])
{
return static::withoutEvents(function () use ($column, $amount, $extra) {
return $this->incrementOrDecrement($column, $amount, $extra, 'increment');
});
return static::withoutEvents(
fn () => $this->incrementOrDecrement($column, $amount, $extra, 'increment')
);
}

/**
Expand All @@ -1073,9 +1073,9 @@ protected function incrementQuietly($column, $amount = 1, array $extra = [])
*/
protected function decrementQuietly($column, $amount = 1, array $extra = [])
{
return static::withoutEvents(function () use ($column, $amount, $extra) {
return $this->incrementOrDecrement($column, $amount, $extra, 'decrement');
});
return static::withoutEvents(
fn () => $this->incrementOrDecrement($column, $amount, $extra, 'decrement')
);
}

/**
Expand Down Expand Up @@ -1732,10 +1732,10 @@ public function refresh()
->attributes
);

$this->load((new BaseCollection($this->relations))->reject(function ($relation) {
return $relation instanceof Pivot
|| (is_object($relation) && in_array(AsPivot::class, class_uses_recursive($relation), true));
})->keys()->all());
$this->load((new BaseCollection($this->relations))->reject(
fn ($relation) => $relation instanceof Pivot
|| (is_object($relation) && in_array(AsPivot::class, class_uses_recursive($relation), true))
)->keys()->all());

$this->syncOriginal();

Expand Down Expand Up @@ -2280,11 +2280,15 @@ public function __set($key, $value)
*/
public function offsetExists($offset): bool
{
try {
return ! is_null($this->getAttribute($offset));
} catch (MissingAttributeException) {
return false;
}
$shouldPrevent = static::$modelsShouldPreventAccessingMissingAttributes;

static::$modelsShouldPreventAccessingMissingAttributes = false;

$result = ! is_null($this->getAttribute($offset));

static::$modelsShouldPreventAccessingMissingAttributes = $shouldPrevent;

return $result;
}

/**
Expand Down
17 changes: 17 additions & 0 deletions Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,23 @@ public function find($id, $columns = ['*'])
)->first($columns);
}

/**
* Find a sole related model by its primary key.
*
* @param mixed $id
* @param array $columns
* @return TRelatedModel
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
* @throws \Illuminate\Database\MultipleRecordsFoundException
*/
public function findSole($id, $columns = ['*'])
{
return $this->where(
$this->getRelated()->getQualifiedKeyName(), '=', $this->parseId($id)
)->sole($columns);
}

/**
* Find multiple related models by their primary keys.
*
Expand Down
17 changes: 17 additions & 0 deletions Eloquent/Relations/HasOneOrManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,23 @@ public function find($id, $columns = ['*'])
)->first($columns);
}

/**
* Find a sole related model by its primary key.
*
* @param mixed $id
* @param array $columns
* @return TRelatedModel
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
* @throws \Illuminate\Database\MultipleRecordsFoundException
*/
public function findSole($id, $columns = ['*'])
{
return $this->where(
$this->getRelated()->getQualifiedKeyName(), '=', $id
)->sole($columns);
}

/**
* Find multiple related models by their primary keys.
*
Expand Down
6 changes: 4 additions & 2 deletions Eloquent/Relations/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ public function __construct(Builder $query, Model $parent)
/**
* Run a callback with constraints disabled on the relation.
*
* @param \Closure $callback
* @return mixed
* @template TReturn of mixed
*
* @param Closure(): TReturn $callback
* @return TReturn
*/
public static function noConstraints(Closure $callback)
{
Expand Down
2 changes: 1 addition & 1 deletion Query/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3161,7 +3161,7 @@ public function paginate($perPage = 15, $columns = ['*'], $pageName = 'page', $p

$total = value($total) ?? $this->getCountForPagination();

$perPage = $perPage instanceof Closure ? $perPage($total) : $perPage;
$perPage = value($perPage, $total);

$results = $total ? $this->forPage($page, $perPage)->get($columns) : new Collection;

Expand Down

0 comments on commit d930357

Please sign in to comment.