Skip to content

Commit

Permalink
[10.x] Make sure pivot model has previously defined values (#46559)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo authored Mar 28, 2023
1 parent 0fc5d30 commit e1e796b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
47 changes: 47 additions & 0 deletions tests/Integration/Database/EloquentPivotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -106,3 +144,12 @@ class PivotTestContributor extends Pivot
'permissions' => 'json',
];
}

class PivotTestSubscription extends Pivot
{
public $table = 'subscriptions';

protected $attributes = [
'status' => 'active',
];
}

0 comments on commit e1e796b

Please sign in to comment.