Laravel adding data to pivot table while inserting new record Laravel adding data to pivot table while inserting new record laravel laravel

Laravel adding data to pivot table while inserting new record


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.