abkrim
4 months ago

Understanding Eager Loading with lineal relationship

Posted 4 months ago by abkrim

Hi.

I have three models

User has a hasmany relation (domains()) with Domain and this hasMany relation (mailboxes) with Mailbox

User.php

public function domains()
{
    return $this->hasMany(Domain::class);
}

Domain.php

public function user()
{
    return $this->belongsTo(User::class);
}

public function mailboxes()
{
    return $this->hasMany(Mailbox::class, 'domain', 'domain');
}

Mailbox.php

public function domains()
{
    return $this->belongsTo('App\Domain', 'domain', 'domain');
}

I need create a query for return only mailboxes owned of user for use in mi Policies.

I tried User::find($user)->with('domains.mailboxes')->get() but is not correct.

Any ideas?

Please sign in or create an account to participate in this conversation.