You can query pivot table this way:
$user->accounts()->whereIn('account_user_account_roles.role_id', [2,3])->get();
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.
How do you know it fails on pivot table? From what I know it would throw exception if that happened.
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community