Skip to content

Commit e31d131

Browse files
committed
formatting
1 parent 35f6cc8 commit e31d131

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

+20-17
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
trait InteractsWithPivotTable
1111
{
1212
/**
13+
* The cached copy of the currently attached pivot models.
14+
*
1315
* @var Collection
1416
*/
15-
private $current;
17+
private $currentlyAttached;
1618

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

99102
$detach = array_diff($current, array_keys(
100103
$records = $this->formatRecordsList($this->parseIds($ids))
@@ -217,7 +220,7 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
217220
*/
218221
protected function updateExistingPivotUsingCustomClass($id, array $attributes, $touch)
219222
{
220-
$updated = $this->getCurrent()
223+
$updated = $this->getCurrentlyAttachedPivots()
221224
->where($this->foreignPivotKey, $this->parent->{$this->parentKey})
222225
->where($this->relatedPivotKey, $this->parseId($id))
223226
->first()
@@ -465,6 +468,20 @@ protected function detachUsingCustomClass($ids)
465468
return $results;
466469
}
467470

471+
/**
472+
* Get the pivot models that are currently attached.
473+
*
474+
* @return \Illuminate\Support\Collection
475+
*/
476+
protected function getCurrentlyAttachedPivots()
477+
{
478+
return $this->currentlyAttached ?: $this->newPivotQuery()->get()->map(function ($record) {
479+
$class = $this->using ? $this->using : Pivot::class;
480+
481+
return (new $class)->setRawAttributes((array) $record, true);
482+
});
483+
}
484+
468485
/**
469486
* Create a new pivot model instance.
470487
*
@@ -645,18 +662,4 @@ protected function getTypeSwapValue($type, $value)
645662
return $value;
646663
}
647664
}
648-
649-
/**
650-
* Get the existing records.
651-
*
652-
* @return \Illuminate\Support\Collection
653-
*/
654-
protected function getCurrent()
655-
{
656-
return $this->current ?: $this->newPivotQuery()->get()->map(function ($record) {
657-
$class = $this->using ? $this->using : Pivot::class;
658-
659-
return (new $class)->setRawAttributes((array) $record, true);
660-
});
661-
}
662665
}

0 commit comments

Comments
 (0)