Skip to content
Merged
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
59 changes: 13 additions & 46 deletions src/Illuminate/Database/Eloquent/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Concerns\HasDefault;

class BelongsTo extends Relation
{
/**
* Indicates if a default model instance should be used.
*
* Alternatively, may be a Closure or array.
*
* @var \Closure|array|bool
*/
protected $withDefault;
use HasDefault;

/**
* The child model instance of the relation.
Expand Down Expand Up @@ -164,31 +158,6 @@ public function initRelation(array $models, $relation)
return $models;
}

/**
* 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;
}

$instance = $this->related->newInstance();

if (is_callable($this->withDefault)) {
return call_user_func($this->withDefault, $instance) ?: $instance;
}

if (is_array($this->withDefault)) {
$instance->forceFill($this->withDefault);
}

return $instance;
}

/**
* Match the eagerly loaded results to their parents.
*
Expand Down Expand Up @@ -327,19 +296,6 @@ protected function relationHasIncrementingId()
$this->related->getKeyType() === 'int';
}

/**
* Return a new model instance in case the relationship does not exist.
*
* @param \Closure|array|bool $callback
* @return $this
*/
public function withDefault($callback = true)
{
$this->withDefault = $callback;

return $this;
}

/**
* Get the foreign key of the relationship.
*
Expand Down Expand Up @@ -389,4 +345,15 @@ public function getRelation()
{
return $this->relation;
}

/**
* Make a new related instance for the given model.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model
*/
protected function newRelatedInstanceFor(Model $parent)
{
return $this->related->newInstance();
}
}
63 changes: 63 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/Concerns/HasDefault.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Illuminate\Database\Eloquent\Relations\Concerns;

use Illuminate\Database\Eloquent\Model;

trait HasDefault
{
/**
* Indicates if a default model instance should be used.
*
* Alternatively, may be a Closure or array.
*
* @var \Closure|array|bool
*/
protected $withDefault;

/**
* Return a new model instance in case the relationship does not exist.
*
* @param \Closure|array|bool $callback
* @return $this
*/
public function withDefault($callback = true)
{
$this->withDefault = $callback;

return $this;
}

/**
* Get the default value for this relation.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model|null
*/
protected function getDefaultFor(Model $parent)
{
if (! $this->withDefault) {
return;
}

$instance = $this->newRelatedInstanceFor($parent);

if (is_callable($this->withDefault)) {
return call_user_func($this->withDefault, $instance) ?: $instance;
}

if (is_array($this->withDefault)) {
$instance->forceFill($this->withDefault);
}

return $instance;
}

/**
* Make a new related instance for the given model.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model
*/
abstract protected function newRelatedInstanceFor(Model $parent);
}
50 changes: 8 additions & 42 deletions src/Illuminate/Database/Eloquent/Relations/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Concerns\HasDefault;

class HasOne extends HasOneOrMany
{
/**
* Indicates if a default model instance should be used.
*
* Alternatively, may be a Closure or array.
*
* @var \Closure|array|bool
*/
protected $withDefault;
use HasDefault;

/**
* Get the results of the relationship.
Expand Down Expand Up @@ -42,33 +36,6 @@ public function initRelation(array $models, $relation)
return $models;
}

/**
* 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;
}

$instance = $this->related->newInstance()->setAttribute(
$this->getForeignKeyName(), $model->getAttribute($this->localKey)
);

if (is_callable($this->withDefault)) {
return call_user_func($this->withDefault, $instance) ?: $instance;
}

if (is_array($this->withDefault)) {
$instance->forceFill($this->withDefault);
}

return $instance;
}

/**
* Match the eagerly loaded results to their parents.
*
Expand All @@ -83,15 +50,14 @@ public function match(array $models, Collection $results, $relation)
}

/**
* Return a new model instance in case the relationship does not exist.
* Make a new related instance for the given model.
*
* @param \Closure|array|bool $callback
* @return $this
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model
*/
public function withDefault($callback = true)
public function newRelatedInstanceFor(Model $parent)
{
$this->withDefault = $callback;

return $this;
return $this->related->newInstance()
->setAttribute($this->getForeignKeyName(), $parent->{$this->localKey});
}
}
21 changes: 19 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/MorphOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@

namespace Illuminate\Database\Eloquent\Relations;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\Concerns\HasDefault;

class MorphOne extends MorphOneOrMany
{
use HasDefault;

/**
* Get the results of the relationship.
*
* @return mixed
*/
public function getResults()
{
return $this->query->first();
return $this->query->first() ?: $this->getDefaultFor($this->parent);
}

/**
Expand All @@ -26,7 +30,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;
Expand All @@ -44,4 +48,17 @@ public function match(array $models, Collection $results, $relation)
{
return $this->matchOne($models, $results, $relation);
}

/**
* Make a new related instance for the given model.
*
* @param \Illuminate\Database\Eloquent\Model $parent
* @return \Illuminate\Database\Eloquent\Model
*/
public function newRelatedInstanceFor(Model $parent)
{
return $this->related->newInstance()
->setAttribute($this->getForeignKeyName(), $parent->{$this->localKey})
->setAttribute($this->getMorphType(), $this->morphClass);
}
}