-
Notifications
You must be signed in to change notification settings - Fork 10.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.3] Allow Eloquent has one relations to return a default new model #16198
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,18 +2,38 @@ | |
|
||
namespace Illuminate\Database\Eloquent\Relations; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Collection; | ||
|
||
class HasOne extends HasOneOrMany | ||
{ | ||
/** | ||
* Determine whether getResults should return a default new model instance or not. | ||
* | ||
* @var bool | ||
*/ | ||
protected $withDefault = false; | ||
|
||
/** | ||
* Return a new model instance in case the relationship does not exist. | ||
* | ||
* @return $this | ||
*/ | ||
public function withDefault() | ||
{ | ||
$this->withDefault = true; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get the results of the relationship. | ||
* | ||
* @return mixed | ||
*/ | ||
public function getResults() | ||
{ | ||
return $this->query->first(); | ||
return $this->query->first() ?: $this->getDefaultFor($this->parent); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't it return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if you don't return anything, the method will return |
||
|
||
/** | ||
|
@@ -26,7 +46,7 @@ public function getResults() | |
public function initRelation(array $models, $relation) | ||
{ | ||
foreach ($models as $model) { | ||
$model->setRelation($relation, null); | ||
$model->setRelation($relation, $this->getDefaultFor($model)); | ||
} | ||
|
||
return $models; | ||
|
@@ -35,7 +55,7 @@ public function initRelation(array $models, $relation) | |
/** | ||
* Match the eagerly loaded results to their parents. | ||
* | ||
* @param array $models | ||
* @param array $models | ||
* @param \Illuminate\Database\Eloquent\Collection $results | ||
* @param string $relation | ||
* @return array | ||
|
@@ -44,4 +64,19 @@ public function match(array $models, Collection $results, $relation) | |
{ | ||
return $this->matchOne($models, $results, $relation); | ||
} | ||
|
||
/** | ||
* Get the default value for this relation. | ||
* | ||
* @param \Illuminate\Database\Eloquent\Model $model | ||
* @return \Illuminate\Database\Eloquent\Model|null | ||
*/ | ||
protected function getDefaultFor(Model $model) | ||
{ | ||
if ($this->withDefault) { | ||
return $this->related->newInstance()->setAttribute( | ||
$this->getPlainForeignKey(), $model->getAttribute($this->localKey) | ||
); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This variable doesn't determine anything, rather, the description should be just "If getResults should return a default model instance."