Skip to content
This repository has been archived by the owner on Jul 16, 2021. It is now read-only.

[Proposal] Default values on pivot table #872

Closed
sushchyk opened this issue Nov 8, 2017 · 5 comments
Closed

[Proposal] Default values on pivot table #872

sushchyk opened this issue Nov 8, 2017 · 5 comments

Comments

@sushchyk
Copy link

sushchyk commented Nov 8, 2017

When you define a Many-To-Many relationship in your model, you can use wherePivot method for filtering it.
Some people use this feature, and create relationships like these in models:

class Club extends Model
{
    ....
    public function admins()
    {
        return $this->belongsToMany(User::class)->wherePivot('is_admin', 1);
    }

    public function members()
    {
        return $this->belongsToMany(User::class);
    }
   ...
}

But when you save relationship you still have to set pivot data:
$club->admins()->save($user, ['is_admin' => 1]);

I think, it confusing at first sight, and a little tedious because you always need to define pivot data if it different from default table values.
And I didn't find the way to set default pivot values on Many-To-Many relationship in documentation actually.
Also, some people(link, link) asked how to do it, but they didn't get the answer.

So my proposal is to allow to define default pivot values, which will be used both for filtering and for saving:
Something like this:
$this->belongsToMany(User::class)->pivotValues(['is_admin' => 1]);

@zohaibhassan156
Copy link

Is there any to set default value of additional attributes??
Something like I want to set default status id

    public function equipments()
    {
        return $this->belongsToMany(Inventory::class, 'ticket_equipments')
            ->withPivot(['billing_type_id', 'amount', 'date_time_returned', 'status_id'=>TicketEquipment::STATUS_PENDING])
            ->withTimestamps();
    }

like default attributes on Model


    protected $attributes = [
        'status_id' => self::STATUS_PENDING,
    ];


@Stalinko
Copy link

Why is it closed? This proposal wasn't implemented. It is still impossible to specify default attributes for a Pivot.

@PEscar
Copy link

PEscar commented Apr 5, 2020

my case:
i add an extra column uuid in every table of my db, as unique column.
then, i build a "Base" model, extending Model, and in the constructor:
public function __construct(array $attributes = []){

	$this->uuid = Str::uuid();
    parent::__construct($attributes);
}

All OK until here.

Now, i have some polimorphic relations, and i cant do a
$model_instance->relation()->attach([$polimorfic_id]);

because it dont call the constructor method, and i receive this exception:

General error: 1364 Field 'uuid' doesn't have a default value (SQL: insert into ......

Workaround ?? I like to use attach / sync / and variations on my polimorfic relations...

@forrestedw
Copy link

I'd also like to be able to set a default value on a custom pivot. I like @zohaibhassan156's suggestion.

@bilogic
Copy link

bilogic commented Jan 16, 2021

Same here, can we have it like @zohaibhassan156's suggestion?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants