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

jak645's avatar

5 table relation $?%$&

Hello people i need help with a multi table relationship. But I cannot get it =(

for one entry in table 1 I need to find all items related to im in table 5 :O

and table 4 and table 3 and table 2

in different eloquent request...

how can I do jump 5 tables.

please explain your logic ^_^

https://drive.google.com/file/d/10_ZpD8lPRlf3P8qCFmlUAfQj_jgAGrbG/view?usp=sharing

0 likes
14 replies
Sergiu17's avatar

Hi,

Table1::with('table2.table3.table4.table5')->get();

Like so

jak645's avatar

This return me a full json off all table but i want only table 5 item in my json. Else i can concat all array of table 5 together to make my full table 5 item. Do you have something more optimize ?

Sergiu17's avatar

@jak645 something is wrong here if you want to go from table1 to table5 through 3 tables ( just to grab table 5 ). Can you add for example relationships between table1 and table5?

Snapey's avatar

You don't seem to have anything relating table 1 and table 2 ?

jak645's avatar

Join table between 1 and 2 but i miss write my join table

Tabe 1 <--> Table 2 Contain Table1_id : Table2_id

Snapey's avatar

So table 1 can have many table 2

Therefore table 1 to table 3 will be many table 3.

Without real names for things its really difficult to illustrate

$table5s = Table1::table2s()->table3()->table4()->table5()->get();

jak645's avatar

There my table

Users, Users_Homes, Homes, Rooms, Box, Items,

Users Homes have many to many relation. Rooms have one Homes. Box have one Rooms. Items have one Box.

i try to get all Items from users ^^

Snapey's avatar

Sorry, you need to specify user

eg,

$items = Auth::user()->homes()->rooms()->boxes()->items();

or if you have user

$items = $user->homes()->rooms()->boxes()->items();
jak645's avatar

Method Illuminate\Database\Query\Builder::rooms does not exist.

Method rooms() is existing in homes() model

$user = User::find($id); return $user->homes()->rooms()->box()->get();

jak645's avatar
return Box::whereHas('rooms',function ($query){
            $query->whereHas('homes', function ($query){
                $query->whereHas('users', function ($query){
                    $query->where('users_id','=' ,1);
                });
            });
        })->get();

damm how i can optimize this

jak645's avatar
class Home extends Model
{
    use SoftDeletes;

    protected $table = 'homes';

    protected $fillable = ['title','description','adresse'];

    protected $guarded = ['id','deleted_at'];

    public function users(){
        return $this->belongsToMany('App\User','homes_users','homes_id','users_id');
    }

    public function rooms(){
        return $this->hasMany('App\Room','home_id');
    }
}
jak645's avatar
jak645
OP
Best Answer
Level 1

return Box::whereHas('rooms.homes.users', function ($q) { $q->where('users_id',$this->id); });

1 like

Please or to participate in this conversation.