From e1e796b6400bef3238f33ac9d25c5b7bb3448b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Tue, 28 Mar 2023 20:03:00 +0200 Subject: [PATCH] [10.x] Make sure pivot model has previously defined values (#46559) --- .../Concerns/InteractsWithPivotTable.php | 2 + .../Database/EloquentPivotTest.php | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php index 2241719b7b37..ed8a9b7ba8a1 100644 --- a/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php +++ b/src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php @@ -510,6 +510,8 @@ protected function getCurrentlyAttachedPivots() */ public function newPivot(array $attributes = [], $exists = false) { + $attributes = array_merge(array_column($this->pivotValues, 'value', 'column'), $attributes); + $pivot = $this->related->newPivot( $this->parent, $attributes, $this->table, $exists, $this->using ); diff --git a/tests/Integration/Database/EloquentPivotTest.php b/tests/Integration/Database/EloquentPivotTest.php index 543c40e1b52b..e673b459000d 100644 --- a/tests/Integration/Database/EloquentPivotTest.php +++ b/tests/Integration/Database/EloquentPivotTest.php @@ -35,6 +35,12 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed() $table->integer('project_id'); $table->text('permissions')->nullable(); }); + + Schema::create('subscriptions', function (Blueprint $table) { + $table->integer('user_id'); + $table->integer('project_id'); + $table->string('status'); + }); } public function testPivotConvenientHelperReturnExpectedResult() @@ -60,11 +66,43 @@ public function testPivotConvenientHelperReturnExpectedResult() $this->assertSame('project_id', $pivot->getForeignKey()); }); } + + public function testPivotValuesCanBeSetFromRelationDefinition() + { + $user = PivotTestUser::forceCreate(['email' => 'taylor@laravel.com']); + $active = PivotTestProject::forceCreate(['name' => 'Active Project']); + $inactive = PivotTestProject::forceCreate(['name' => 'Inactive Project']); + + $this->assertSame('active', $user->activeSubscriptions()->newPivot()->status); + $this->assertSame('inactive', $user->inactiveSubscriptions()->newPivot()->status); + + $user->activeSubscriptions()->attach($active); + $user->inactiveSubscriptions()->attach($inactive); + + $this->assertSame('active', $user->activeSubscriptions->first()->pivot->status); + $this->assertSame('inactive', $user->inactiveSubscriptions->first()->pivot->status); + } } class PivotTestUser extends Model { public $table = 'users'; + + public function activeSubscriptions() + { + return $this->belongsToMany(PivotTestProject::class, 'subscriptions', 'user_id', 'project_id') + ->withPivotValue('status', 'active') + ->withPivot('status') + ->using(PivotTestSubscription::class); + } + + public function inactiveSubscriptions() + { + return $this->belongsToMany(PivotTestProject::class, 'subscriptions', 'user_id', 'project_id') + ->withPivotValue('status', 'inactive') + ->withPivot('status') + ->using(PivotTestSubscription::class); + } } class PivotTestProject extends Model @@ -106,3 +144,12 @@ class PivotTestContributor extends Pivot 'permissions' => 'json', ]; } + +class PivotTestSubscription extends Pivot +{ + public $table = 'subscriptions'; + + protected $attributes = [ + 'status' => 'active', + ]; +}