Laravel adding data to pivot table while inserting new record
Asked Answered
N

2

35

I have three tables,

roles(id, name);
users(id, email, password);
user_role(id, user_id, role_id);

In this scenario, I have users table is associated to roles table with many to many relation.

I have two eloquent model as

Role
+users(){
    belongsToMany('User')
}

User
+roles(){
    belongsToMany('Role')
}

Now, the question is when I want to add a new user along with ids of roles that I would like to assign to users. How can I insert values to pivot table with Laravel best practices?

My existing code is:-

$roles = Input::get('roles'); // arrays of role ids

$user = new User();
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();

What to do next???

Neural answered 2/2, 2015 at 9:26 Comment(1)
Just like here: laravel.com/docs/4.2/eloquent#inserting-related-modelsEsquibel
D
42

It's really all described in the documentation.

Anyway, here's how you do it:

$user = new User();
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$user->roles()->attach($roles);

The important part is that you have to save the user before attaching (using attach()) the roles because otherwise the user doesn't have an id yet to be used in the pivot table.

Diazonium answered 2/2, 2015 at 9:37 Comment(9)
I tried it, but it is returning an error, Call to undefined method Illuminate\\Database\\Eloquent\\Collection::attach()Neural
Just to clarify, the parentheses matter! $user->roles() NOT $user->rolesVortex
@Diazonium it's not that clear. How are we to know the attach() needs to be done after the record has been saved?Featureless
@surfer190 Well that's how all eloquent relations work. First you save one side, and then associate the other. But I agree that this might not be very clear from the documentation.Diazonium
The same goes in laravel 5.4 also?Mask
Yes @Mask same goes for 5.4 . Need to insert one record before trying to associate it with another since the association uses the primary key from the parent to insert into the child so if there hasn't been an insertion, then there will be no id.Brusque
Another clarification, this only works with belongsToMany relationship and not hasManyTwiddle
I am confused, what is the type of $roles variable is it an object or array please clarify that I also read the documentation but is a bit complicated for me?Folkways
Knowing what to look for in the documentation is half the battle for new people.Bellew
F
0

If there are timestamps like created_at and updated_at on the pivot table, you can pass those as another parameter to the attach method like below.

<?php

use Carbon\Carbon;

// Assuming $roles is an array, say like having values as [1,2,80,200] etc.

$user->save();
$user->roles()->attach($roles, array(
   'created_at' => Carbon::now(), // or date("Y-m-d H:i:s")
   'updated_at' => Carbon::now()
));
Fitzpatrick answered 21/8, 2023 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.