Skip to content
Permalink
Browse files

formatting

  • Loading branch information...
taylorotwell committed May 5, 2019
1 parent 35f6cc8 commit e31d13111da02fed6bd2ce7a6393431a4b34f924
Showing with 20 additions and 17 deletions.
  1. +20 −17 src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php
@@ -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.
@@ -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))
@@ -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()
@@ -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.
*
@@ -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.
You can’t perform that action at this time.