It would be helpful to the discussion if you shared the Eloquent query you have written
Roles pivot table multiple queries
Hi I have checked the debug bar and there are a number of duplicate queries for
select exists(select * from `roles` inner join `role_user` on `roles`.`id` = `role_user`.`role_id` where `role_user`.`user_id` = 3 and `name` = 'admin') as `exists`
What can I do to mitigate this? I did some research but there are multiple way of stopping this from happening, on the user model or the App service provider, but I am a bit stuck on the best way to achieve this, thanks
@Cushty just be aware that fix requires the roles relation to be loaded otherwise you reintroduce the N+1 problem again. If you are already eager-loading the roles relation, then you are good. However, depending on your specific needs you can sometimes be more efficient using a JOIN rather than loading entire the Collection of Role instances on every User instance.
Please or to participate in this conversation.