Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed May 5, 2019
1 parent 35f6cc8 commit e31d131
Showing 1 changed file with 20 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
trait InteractsWithPivotTable
{
/**
* The cached copy of the currently attached pivot models.
*
* @var Collection
*/
private $current;
private $currentlyAttached;

/**
* Toggles a model (or models) from the parent.
Expand Down Expand Up @@ -94,7 +96,8 @@ public function sync($ids, $detaching = true)
// First we need to attach any of the associated models that are not currently
// in this joining table. We'll spin through the given IDs, checking to see
// if they exist in the array of current ones, and if not we will insert.
$current = $this->getCurrent()->pluck($this->relatedPivotKey)->all();
$current = $this->getCurrentlyAttachedPivots()
->pluck($this->relatedPivotKey)->all();

$detach = array_diff($current, array_keys(
$records = $this->formatRecordsList($this->parseIds($ids))
Expand Down Expand Up @@ -217,7 +220,7 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
*/
protected function updateExistingPivotUsingCustomClass($id, array $attributes, $touch)
{
$updated = $this->getCurrent()
$updated = $this->getCurrentlyAttachedPivots()
->where($this->foreignPivotKey, $this->parent->{$this->parentKey})
->where($this->relatedPivotKey, $this->parseId($id))
->first()
Expand Down Expand Up @@ -465,6 +468,20 @@ protected function detachUsingCustomClass($ids)
return $results;
}

/**
* Get the pivot models that are currently attached.
*
* @return \Illuminate\Support\Collection
*/
protected function getCurrentlyAttachedPivots()
{
return $this->currentlyAttached ?: $this->newPivotQuery()->get()->map(function ($record) {
$class = $this->using ? $this->using : Pivot::class;

return (new $class)->setRawAttributes((array) $record, true);
});
}

/**
* Create a new pivot model instance.
*
Expand Down Expand Up @@ -645,18 +662,4 @@ protected function getTypeSwapValue($type, $value)
return $value;
}
}

/**
* Get the existing records.
*
* @return \Illuminate\Support\Collection
*/
protected function getCurrent()
{
return $this->current ?: $this->newPivotQuery()->get()->map(function ($record) {
$class = $this->using ? $this->using : Pivot::class;

return (new $class)->setRawAttributes((array) $record, true);
});
}
}

0 comments on commit e31d131

Please sign in to comment.