Laravel Eloquent many-to-many relationship: Use explicit pivot table

F.M.F.

How can I select an explicit table (with migration and model) as pivot table?

My models are

User
-id

Role
-id

user_role
-user_id
-role_id
-other_foreign_key_id
-other_attributes
-...

I know how many-to-many relations work in laravel in general. But in the documentation they only use the automatic generated joining table where I can't define an additional foreign key constraint or datatypes for additional columns.

What I want is that I have my own migration and model for the user_role table and use that as pivot but I don't know how to explicit link a defined table as pivot to a m-to-n relation.

Thanks in advance.

Saumya Rastogi

In addition to customizing the name of the joining table, you may also customize the column names of the keys on the table by passing additional arguments to the belongsToMany method. The third argument is the foreign key name of the model on which you are defining the relationship, while the fourth argument is the foreign key name of the model that you are joining to, like this:

return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id')
           ->withPivot(['other_foreign_key_id', 'other_attributes']);

By default, only the model keys will be present on the pivot object. If your pivot table contains extra attributes, you must specify them when defining the relationship with withPivot() method as done above:

You can use your pivot attributes using pivot like this:

$user = User::find($id);
foreach ($user->roles as $role) {
    echo $role->pivot->created_at;
    echo $role->pivot->other_attribute;
}

UPDATE - Custom pivot model in Eloquent

You can also define your own custom pivot model like this:

class RoleUserPivot extends Pivot {
    // let's use date mutator for a field
    protected $dates = ['completed_at'];
}

Now, in order to let Eloquent grab this pivot model, we also need to override newPivot method on both User and Role:

// User model
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
    if ($parent instanceof Role) {
        return new RoleUserPivot($parent, $attributes, $table, $exists);
    }

    return parent::newPivot($parent, $attributes, $table, $exists);
}


// Role model
public function newPivot(Model $parent, array $attributes, $table, $exists)
{
    if ($parent instanceof User) {
        return new RoleUserPivot($parent, $attributes, $table, $exists);
    }

    return parent::newPivot($parent, $attributes, $table, $exists);
}

See more about Custom Pivot Tables

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

From Dev

Laravel - Order by pivot value in Many to Many table relationship

From Dev

Unable to sync conditional pivot table many to many relationship laravel

From Dev

Laravel L5.5 No access to pivot table in "Many to many" relationship

From Dev

Laravel eloquent one to many relationship in custom pivot tables

From Dev

Eloquent - Many to many where not found in the pivot table

From Dev

Eloquent many to many relationship access table column value from pivot table

From Dev

Laravel Removing Pivot data in many to many relationship

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Creating one to many relationship with pivot table of many to many relationship

From Dev

Soft deleting a many-to-many relationship's pivot in Eloquent

From Dev

Soft deleting a many-to-many relationship's pivot in Eloquent

From Dev

Laravel 4 - Many to Many - pivot table not updating

From Dev

Laravel Many-to-Many Pivot Table

From Dev

Laravel Eloquent many to many filter on intermediate table

From Dev

Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

From Dev

Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

From Dev

Laravel 4.2 Many-to-many relationship : Can't read from pivot table

From Dev

How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

From Dev

laravel update, remove one to many relationship eloquent

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

One to Many to One relationship in laravel eloquent

Related Related

  1. 1

    Laravel Eloquent Many to Many relationship using a pivot table

  2. 2

    Laravel belongsToMany inserting '0' on pivot table ids in a many to many relationship?

  3. 3

    Laravel - Order by pivot value in Many to Many table relationship

  4. 4

    Unable to sync conditional pivot table many to many relationship laravel

  5. 5

    Laravel L5.5 No access to pivot table in "Many to many" relationship

  6. 6

    Laravel eloquent one to many relationship in custom pivot tables

  7. 7

    Eloquent - Many to many where not found in the pivot table

  8. 8

    Eloquent many to many relationship access table column value from pivot table

  9. 9

    Laravel Removing Pivot data in many to many relationship

  10. 10

    Laravel many to many relationship using Eloquent

  11. 11

    Laravel 5 Eloquent count many to many relationship

  12. 12

    Creating one to many relationship with pivot table of many to many relationship

  13. 13

    Creating one to many relationship with pivot table of many to many relationship

  14. 14

    Soft deleting a many-to-many relationship's pivot in Eloquent

  15. 15

    Soft deleting a many-to-many relationship's pivot in Eloquent

  16. 16

    Laravel 4 - Many to Many - pivot table not updating

  17. 17

    Laravel Many-to-Many Pivot Table

  18. 18

    Laravel Eloquent many to many filter on intermediate table

  19. 19

    Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

  20. 20

    Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

  21. 21

    Laravel 4.2 Many-to-many relationship : Can't read from pivot table

  22. 22

    How to get all books from another table when using many to many relationship Laravel / Eloquent / Query builder

  23. 23

    laravel update, remove one to many relationship eloquent

  24. 24

    Laravel eloquent update one-many relationship

  25. 25

    Laravel 5 Eloquent Relationship - Has Many Though

  26. 26

    Laravel eloquent update one-many relationship

  27. 27

    Laravel 5 one to many eloquent relationship

  28. 28

    Laravel 5 Eloquent Relationship - Has Many Though

  29. 29

    One to Many to One relationship in laravel eloquent

HotTag

Archive