Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 52 additions & 9 deletions src/Illuminate/Database/Eloquent/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@ class Builder implements BuilderContract
*/
protected $removedScopes = [];

/**
* The models that the queried models will be related to via a foreign key.
*
* @var array
*/
protected $for = [];

/**
* Create a new Eloquent query builder instance.
*
Expand All @@ -146,7 +153,7 @@ public function __construct(QueryBuilder $query)
*/
public function make(array $attributes = [])
{
return $this->newModelInstance($attributes);
return $this->newModelInstance($this->mergeForeignKeys($attributes));
}

/**
Expand Down Expand Up @@ -530,11 +537,11 @@ public function findOr($id, $columns = ['*'], Closure $callback = null)
*/
public function firstOrNew(array $attributes = [], array $values = [])
{
if (! is_null($instance = $this->where($attributes)->first())) {
if (! is_null($instance = $this->where($this->mergeForeignKeys($attributes))->first())) {
return $instance;
}

return $this->newModelInstance(array_merge($attributes, $values));
return $this->newModelInstance($this->mergeForeignKeys(array_merge($attributes, $values)));
}

/**
Expand All @@ -546,11 +553,11 @@ public function firstOrNew(array $attributes = [], array $values = [])
*/
public function firstOrCreate(array $attributes = [], array $values = [])
{
if (! is_null($instance = $this->where($attributes)->first())) {
if (! is_null($instance = $this->where($this->mergeForeignKeys($attributes))->first())) {
return $instance;
}

return tap($this->newModelInstance(array_merge($attributes, $values)), function ($instance) {
return tap($this->newModelInstance($this->mergeForeignKeys(array_merge($attributes, $values))), function ($instance) {
$instance->save();
});
}
Expand All @@ -565,7 +572,7 @@ public function firstOrCreate(array $attributes = [], array $values = [])
public function updateOrCreate(array $attributes, array $values = [])
{
return tap($this->firstOrNew($attributes), function ($instance) use ($values) {
$instance->fill($values)->save();
$instance->fill($this->mergeForeignKeys($values))->save();
});
}

Expand Down Expand Up @@ -970,7 +977,7 @@ protected function ensureOrderForCursorPagination($shouldReverse = false)
*/
public function create(array $attributes = [])
{
return tap($this->newModelInstance($attributes), function ($instance) {
return tap($this->newModelInstance($this->mergeForeignKeys($attributes)), function ($instance) {
$instance->save();
});
}
Expand All @@ -984,7 +991,7 @@ public function create(array $attributes = [])
public function forceCreate(array $attributes)
{
return $this->model->unguarded(function () use ($attributes) {
return $this->newModelInstance()->create($attributes);
return $this->newModelInstance()->create($this->mergeForeignKeys($attributes));
});
}

Expand All @@ -996,7 +1003,7 @@ public function forceCreate(array $attributes)
*/
public function update(array $values)
{
return $this->toBase()->update($this->addUpdatedAtColumn($values));
return $this->toBase()->update($this->addUpdatedAtColumn($this->mergeForeignKeys($values)));
}

/**
Expand Down Expand Up @@ -1140,6 +1147,21 @@ protected function addUpdatedAtToUpsertColumns(array $update)
return $update;
}

/**
* Merge the attributes that will be used in the query with the specific foreign keys.
*
* @param array $attributes
* @return array
*/
protected function mergeForeignKeys($attributes)
{
foreach ($this->for as $model) {
$attributes[$model['foreignKey']] = $model['model']->getKey();
}

return $attributes;
}

/**
* Delete records from the database.
*
Expand Down Expand Up @@ -1409,6 +1431,27 @@ public function withOnly($relations)
return $this->with($relations);
}

/**
* Set the foreign key for a relationship to another model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string|null $relationship
* @return $this
*/
public function for($model, $relationship = null)
{
$relationship ??= Str::camel(class_basename($model));

$foreignKey = $this->model->{$relationship}()->getForeignKeyName();

$this->for[] = [
'model' => $model,
'foreignKey' => $foreignKey,
];

return $this;
}

/**
* Create a new instance of the model being queried.
*
Expand Down
28 changes: 28 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,24 @@ public function forceFill(array $attributes)
});
}

/**
* Set the foreign key for a relationship to another model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string|null $relationship
* @return $this
*/
public function forModel($model, $relationship = null)
{
$relationship ??= Str::camel(class_basename($model));

$foreignKey = $this->{$relationship}()->getForeignKeyName();

$this->fill([$foreignKey => $model->getKey()]);

return $this;
}

/**
* Qualify the given column name by the model's table.
*
Expand Down Expand Up @@ -2156,6 +2174,10 @@ public function __call($method, $parameters)
return $this->$method(...$parameters);
}

if ($method === 'for') {
return $this->forModel(...$parameters);
}

if ($resolver = (static::$relationResolvers[get_class($this)][$method] ?? null)) {
return $resolver($this);
}
Expand All @@ -2172,6 +2194,12 @@ public function __call($method, $parameters)
*/
public static function __callStatic($method, $parameters)
{
if ($method === 'for') {
$model = new static;

return $model->forwardCallTo($model->newQuery(), $method, $parameters);
}

return (new static)->$method(...$parameters);
}

Expand Down
56 changes: 50 additions & 6 deletions src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithDictionary;
use Illuminate\Support\Str;

abstract class HasOneOrMany extends Relation
{
Expand All @@ -25,6 +26,13 @@ abstract class HasOneOrMany extends Relation
*/
protected $localKey;

/**
* The models that the queried models will be related to via a foreign key.
*
* @var array
*/
protected $for = [];

/**
* Create a new has one or many relationship instance.
*
Expand All @@ -50,7 +58,7 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $localKe
*/
public function make(array $attributes = [])
{
return tap($this->related->newInstance($attributes), function ($instance) {
return tap($this->related->newInstance($this->mergeForeignKeys($attributes)), function ($instance) {
$this->setForeignAttributesForCreate($instance);
});
}
Expand Down Expand Up @@ -186,6 +194,21 @@ protected function buildDictionary(Collection $results)
})->all();
}

/**
* Merge the attributes that will be used in the query with the specific foreign keys.
*
* @param array $attributes
* @return array
*/
protected function mergeForeignKeys($attributes)
{
foreach ($this->for as $model) {
$attributes[$model['foreignKey']] = $model['model']->getKey();
}

return $attributes;
}

/**
* Find a model by its primary key or return a new instance of the related model.
*
Expand Down Expand Up @@ -213,8 +236,8 @@ public function findOrNew($id, $columns = ['*'])
*/
public function firstOrNew(array $attributes = [], array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
$instance = $this->related->newInstance(array_merge($attributes, $values));
if (is_null($instance = $this->where($this->mergeForeignKeys($attributes))->first())) {
$instance = $this->related->newInstance($this->mergeForeignKeys(array_merge($attributes, $values)));

$this->setForeignAttributesForCreate($instance);
}
Expand All @@ -231,7 +254,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
*/
public function firstOrCreate(array $attributes = [], array $values = [])
{
if (is_null($instance = $this->where($attributes)->first())) {
if (is_null($instance = $this->where($this->mergeForeignKeys($attributes))->first())) {
$instance = $this->create(array_merge($attributes, $values));
}

Expand Down Expand Up @@ -295,6 +318,27 @@ public function saveMany($models)
return $models;
}

/**
* Set the foreign key for a relationship to another model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string|null $relationship
* @return $this
*/
public function for($model, $relationship = null)
{
$relationship ??= Str::camel(class_basename($model));

$foreignKey = $this->getModel()->{$relationship}()->getForeignKeyName();

$this->for[] = [
'model' => $model,
'foreignKey' => $foreignKey,
];

return $this;
}

/**
* Create a new instance of the related model.
*
Expand All @@ -303,7 +347,7 @@ public function saveMany($models)
*/
public function create(array $attributes = [])
{
return tap($this->related->newInstance($attributes), function ($instance) {
return tap($this->related->newInstance($this->mergeForeignKeys($attributes)), function ($instance) {
$this->setForeignAttributesForCreate($instance);

$instance->save();
Expand All @@ -320,7 +364,7 @@ public function forceCreate(array $attributes = [])
{
$attributes[$this->getForeignKeyName()] = $this->getParentKey();

return $this->related->forceCreate($attributes);
return $this->related->forceCreate($this->mergeForeignKeys($attributes));
}

/**
Expand Down
Loading