Skip to content

Commit

Permalink
remove a few features and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Nov 9, 2016
1 parent 5ebc81e commit 5459777
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 70 deletions.
47 changes: 15 additions & 32 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class BelongsToMany extends Relation
protected $pivotUpdatedAt;

/**
* The custom pivot model to use.
* The class name of the custom pivot model to use for the relationship.
*
* @var string
*/
Expand Down Expand Up @@ -109,6 +109,19 @@ public function __construct(Builder $query, Model $parent, $table, $foreignKey,
parent::__construct($query, $parent);
}

/**
* Specify the custom pivot model to use for the relationship.
*
* @param string $class
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function using($class)
{
$this->using = $class;

return $this;
}

/**
* Get the results of the relationship.
*
Expand Down Expand Up @@ -237,19 +250,6 @@ public function get($columns = ['*'])
return $this->related->newCollection($models);
}

/**
* Set a custom pivot model to use.
*
* @param string $pivotModelName
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function using($pivotModelName)
{
$this->using = $pivotModelName;

return $this;
}

/**
* Get a paginator for the "select" statement.
*
Expand Down Expand Up @@ -443,30 +443,13 @@ protected function getAliasedPivotColumns()
// relationships when they are retrieved and hydrated into the models.
$columns = [];

foreach (array_merge($defaults, $this->getPivotModelColumns(), $this->pivotColumns) as $column) {
foreach (array_merge($defaults, $this->pivotColumns) as $column) {
$columns[] = $this->table.'.'.$column.' as pivot_'.$column;
}

return array_unique($columns);
}

/**
* Retrieve necessary columns from the custom pivot model.
*
* @return array
*/
protected function getPivotModelColumns()
{
$columns = [];

if ($this->using) {
$customPivotClass = $this->using;
$columns = $customPivotClass::getPivotColumns();
}

return $columns;
}

/**
* Determine whether the given column is defined as a pivot column.
*
Expand Down
31 changes: 0 additions & 31 deletions src/Illuminate/Database/Eloquent/Relations/Pivot.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,6 @@ class Pivot extends Model
*/
protected $guarded = [];

/**
* The attributes that should be loaded into the pivot model.
*
* @var array
*/
protected $include = [];

/**
* Create a new pivot model instance.
*
Expand All @@ -67,8 +60,6 @@ public function __construct(Model $parent, $attributes, $table, $exists = false)

$this->syncOriginal();

$this->eagerLoadRelations();

// We store off the parent instance so we will access the timestamp column names
// for the model, since the pivot model timestamps aren't easily configurable
// from the developer's point of view. We can use the parents to get these.
Expand Down Expand Up @@ -110,28 +101,6 @@ protected function setKeysForSaveQuery(Builder $query)
return $query->where($this->otherKey, $this->getAttribute($this->otherKey));
}

/**
* Eager load any defined relations.
*
* @return null
*/
protected function eagerLoadRelations()
{
foreach ($this->with as $relation) {
$this->relations[$relation] = $this->$relation()->getResults();
}
}

/**
* Retrieve columns required by custom pivot model.
*
* @return array
*/
public static function getPivotColumns()
{
return (new ReflectionClass(static::class))->getDefaultProperties()['include'];
}

/**
* Delete the pivot model record from the database.
*
Expand Down
9 changes: 2 additions & 7 deletions tests/Database/DatabaseEloquentIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,8 @@ class EloquentTestUserWithCustomFriendPivot extends EloquentTestUser
{
public function friends()
{
return $this->belongsToMany('EloquentTestUser', 'friends', 'user_id', 'friend_id')->using('EloquentTestFriendPivot');
return $this->belongsToMany('EloquentTestUser', 'friends', 'user_id', 'friend_id')
->using('EloquentTestFriendPivot')->withPivot('user_id', 'friend_id', 'friend_level_id');
}
}

Expand Down Expand Up @@ -1232,12 +1233,6 @@ class EloquentTestFriendPivot extends Pivot
{
protected $table = 'friends';
protected $guarded = [];
protected $include = [
'user_id', 'friend_id', 'friend_level_id',
];
protected $with = [
'user', 'friend', 'level',
];

public function user()
{
Expand Down

0 comments on commit 5459777

Please sign in to comment.