Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You can query pivot table this way:

$user->accounts()->whereIn('account_user_account_roles.role_id', [2,3])->get();
Last updated 2 years ago.
0

jarektkaczyk said:

You can query pivot table this way:

$user->accounts()->whereIn('account_user_account_roles.role_id', [2,3])->get();

Thanks for you answer before. Is it possible to help me with the insert, as it is not working also. I tried this:

$account = $user->accounts()->save($account, array('role_id' => 2));

It insert the account record in the database, but fail with the relation table

Thank you again.

Last updated 2 years ago.
0

How do you know it fails on pivot table? From what I know it would throw exception if that happened.

Last updated 2 years ago.
0

something really wrong here. when i use the above code, i can see the account record inserted successfully in the database. and i get this error on the page:

QLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`active.estate`.`account_user_account_roles`, CONSTRAINT `FK_A867273BA76ED395` FOREIGN KEY (`user_id`) REFERENCES `account_user` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into `account_user_account_roles` (`account_id`, `role_id`, `user_id`) values (3, 2, 16)) 

now, according to the records in the database 'account_id' should be 16 and 'user_id' should be 3 the opposite of what the query try to insert.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.