-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.6] Default attributes for pivot table during fetching and creating rows #22867
Conversation
This needs a thorough explanation, demonstration of the new features, explanation of benefit to users, etc. |
For example, in this kind of many-to-many relationship: users:
- id
...
clubs:
- id
...
club_user:
- club_id
- user_id
- role With the public function members()
{
return $this->belongsToMany(User::class);
}
public function admins()
{
return $this->belongsToMany(User::class)->wherePivot('role', 'admin');
}
public function staff()
{
return $this->belongsToMany(User::class)->wherePivot('role', 'staff');
} If you want to attach a new administrator for the club, something like: $club->admins()->attach(1); won't work, but rather: $club->admins()->attach(1, ['role' => 'admin']);
OR
$club->members()->attach(1, ['role' => 'admin']);
OR
$club->staff()->attach(1, ['role' => 'admin']); All of these work, but is counter intuitive and some might get confused as to what is happening here since to add an administrator, calling This is where this PR comes in play with the new public function members()
{
return $this->belongsToMany(User::class);
}
public function admins()
{
return $this->belongsToMany(User::class)->withPivotValues('role', 'admin');
}
public function staff()
{
return $this->belongsToMany(User::class)->withPivotValues('role', 'staff');
} Which extends the This would allow the devs to write less in these kind of situations and the syntax is easily understandable. |
This could be solved with polymorphisms or am I missing something? |
@jmarcher polymorphic relationship allows the inclusion of different "content types" in a single table. This PR is to do with different default values and conditions for the same pivot table to create different relationships. |
@jmarcher do you have an example of how you would solve this? Not quite sure what you mean. |
Pretty weird way to set default attributes, don't you find that? The only option now to set the default attributes on a Pivot is call |
@Stalinko not really. It may be highly opinionated, but why define default attributes for a pivot row if that is not going to be used as a filter? |
@sikhlana well there can be various situations. Let's consider the example above with "club members". Imagine you decide to add a new attribute How to implement this default value without filters (hence |
@Stalinko yeah true, that makes sense. I always go with factory classes for this kinda situation, but then again that's a custom solution. There can be a better solution for this. |
@sikhlana yep! When I faced a similar situation I read about default attributes in Eloquent (https://laravel.com/docs/5.8/eloquent#default-attribute-values) and was 100% sure it would work in |
Is withPivotValues not available in "^7.0" ? |
@rdcx It is singular, not plural: |
Is there some way to add a It would be really easy to add a third argument to |
As per the proposal: laravel/ideas#872.
Added a new method so that default pivot values can be set during fetching and creating rows.
This version also includes tests.