Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mnek84's avatar

Eloquent Relation by TableData

Hello, i need an elegant way to filter an eloquent model using relation based on a query.

I have this 3 tables (A B C)
A = id , name,status B = id, a_id,name, type C = id, b_id,type_id,user_id

$query = $A:wherestatus('exampleStatus'); $query->with(['B.C => function($query){ $query->whereuser_id(AutenticatedUserId); }]); $query->get();

I need all the A,B,C data where in the C table had a record given an user_id, so for example when the query returns all data in $a->b collection i should receive only the records that are related to C table. and $a collection should only had the records that are related to C Table

#The Problem with the query is i start using the A table, so will lookup all the A records matching a filter so if i had one A record that is not part of the C table will match the first query even thought the $a->b is empty.

Thanks in advance.!

0 likes
1 reply

Please or to participate in this conversation.