From edb2690b44e1207299c59cb3b72d06f4499e648c Mon Sep 17 00:00:00 2001 From: Laurence Date: Mon, 31 Jul 2017 14:15:02 +0000 Subject: [PATCH 1/2] refactor --- .../Concerns/InteractsWithPivotTable.php | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index 8e63378e66a3..3f9189729528 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -23,7 +23,7 @@ public function toggle($ids, $touch = true) 'attached' => [], 'detached' => [], ]; - $records = $this->formatRecordsList((array) $this->parseIds($ids)); + $records = $this->formatRecordsList($this->parseIds($ids)); // Next, we will determine which IDs should get removed from the join table by // checking which of the given ID/records is in the list of current records @@ -93,7 +93,7 @@ public function sync($ids, $detaching = true) )->all(); $detach = array_diff($current, array_keys( - $records = $this->formatRecordsList((array) $this->parseIds($ids)) + $records = $this->formatRecordsList($this->parseIds($ids)) )); // Next, we will take the differences of the currents and given IDs and detach @@ -211,7 +211,7 @@ public function attach($id, array $attributes = [], $touch = true) // inserted the records, we will touch the relationships if necessary and the // function will return. We can parse the IDs before inserting the records. $this->newPivotStatement()->insert($this->formatAttachRecords( - (array) $this->parseIds($id), $attributes + $this->parseIds($id), $attributes )); if ($touch) { @@ -348,12 +348,14 @@ public function detach($ids = null, $touch = true) // If associated IDs were passed to the method we will only delete those // associations, otherwise all of the association ties will be broken. // We'll return the numbers of affected rows when we do the deletes. - if (! is_null($ids = $this->parseIds($ids))) { - if (count((array) $ids) === 0) { + if (! is_null($ids)) { + $ids = $this->parseIds($ids); + + if (count($ids) === 0) { return 0; } - $query->whereIn($this->relatedKey, (array) $ids); + $query->whereIn($this->relatedKey, $ids); } // Once we have all of the conditions set on the statement, we are ready @@ -455,12 +457,12 @@ public function withPivot($columns) * Get all of the IDs from the given mixed value. * * @param mixed $value - * @return mixed + * @return array */ protected function parseIds($value) { if ($value instanceof Model) { - return $value->getKey(); + return [$value->getKey()]; } if ($value instanceof Collection) { @@ -471,7 +473,7 @@ protected function parseIds($value) return $value->toArray(); } - return $value; + return (array) $value; } /** From bf7ae71d15ee4f4b0c57d4271b8b35574b6cffe7 Mon Sep 17 00:00:00 2001 From: Laurence Date: Mon, 31 Jul 2017 14:40:19 +0000 Subject: [PATCH 2/2] change count to empty --- .../Eloquent/Relations/Concerns/InteractsWithPivotTable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index 3f9189729528..9d4c73f2b976 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -351,7 +351,7 @@ public function detach($ids = null, $touch = true) if (! is_null($ids)) { $ids = $this->parseIds($ids); - if (count($ids) === 0) { + if (empty($ids)) { return 0; }