From 5f520f3e65631c4c6eabbc98ec3d19d7d4e8213e Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Tue, 23 Jan 2018 08:34:14 -0600 Subject: [PATCH] formatting --- .../Eloquent/Relations/BelongsToMany.php | 17 ++++++++++------- .../Concerns/InteractsWithPivotTable.php | 6 ++---- ...ntBelongsToManyWithDefaultAttributesTest.php | 8 ++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php index 61565ba4e214..a5b0cb627255 100755 --- a/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php +++ b/src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php @@ -3,6 +3,7 @@ namespace Illuminate\Database\Eloquent\Relations; use Illuminate\Support\Str; +use InvalidArgumentException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Collection; @@ -76,7 +77,7 @@ class BelongsToMany extends Relation protected $pivotWhereIns = []; /** - * Default values for the pivot columns. + * The default values for the pivot columns. * * @var array */ @@ -355,27 +356,29 @@ public function orWherePivot($column, $operator = null, $value = null) } /** - * Sets default value when querying or creating a new row in the pivot table. + * Set a where clause for a pivot table column. + * + * In addition, new pivot records will receive this value. * * @param string $column - * @param mixed $value + * @param mixed $value * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ - public function withPivotValues($column, $value = null) + public function withPivotValue($column, $value = null) { if (is_array($column)) { foreach ($column as $name => $value) { - $this->withPivotValues($name, $value); + $this->withPivotValue($name, $value); } return $this; } if (is_null($value)) { - throw new \InvalidArgumentException('$value cannot be null.'); + throw new InvalidArgumentException('The provided value may not be null.'); } - $this->pivotValues[] = func_get_args(); + $this->pivotValues[] = compact('column', 'value'); return $this->wherePivot($column, '=', $value); } diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index 952f34005ad8..adc538f8c09b 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -300,10 +300,8 @@ protected function baseAttachRecord($id, $timed) $record = $this->addTimestampsToAttachment($record); } - // Adding the default pivot values (if there is any) to the record. - foreach ($this->pivotValues as $arguments) { - list($name, $value) = $arguments; - $record[$name] = $value; + foreach ($this->pivotValues as $value) { + $record[$value['column']] = $value['value']; } return $record; diff --git a/tests/Database/DatabaseEloquentBelongsToManyWithDefaultAttributesTest.php b/tests/Database/DatabaseEloquentBelongsToManyWithDefaultAttributesTest.php index 8ccec406d66b..9241f7a4f3f6 100644 --- a/tests/Database/DatabaseEloquentBelongsToManyWithDefaultAttributesTest.php +++ b/tests/Database/DatabaseEloquentBelongsToManyWithDefaultAttributesTest.php @@ -12,16 +12,16 @@ public function tearDown() m::close(); } - public function testWithPivotValuesMethodSetsWhereConditionsForFetching() + public function testwithPivotValueMethodSetsWhereConditionsForFetching() { $relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock(); - $relation->withPivotValues(['is_admin' => 1]); + $relation->withPivotValue(['is_admin' => 1]); } - public function testWithPivotValuesMethodSetsDefaultArgumentsForInsertion() + public function testwithPivotValueMethodSetsDefaultArgumentsForInsertion() { $relation = $this->getMockBuilder('Illuminate\Database\Eloquent\Relations\BelongsToMany')->setMethods(['touchIfTouching'])->setConstructorArgs($this->getRelationArguments())->getMock(); - $relation->withPivotValues(['is_admin' => 1]); + $relation->withPivotValue(['is_admin' => 1]); $query = m::mock('stdClass'); $query->shouldReceive('from')->once()->with('club_user')->andReturn($query);