vendredi 27 janvier 2017

Find child model hasMany relation in laravel eloquent

Here is model structure of my Laravel 5.3 project,


  • User.php (Model)

it has one invitation method that returns the invitation of a user.

public function invitations()
{
    return $this->hasMany( 'App\Invitation', 'invitee_id', 'id' );
}


  • Invitation.php (Model)

This model has another method that would return the inviter detail of an invitation.

public function inviter()
{
    return $this->hasOne( 'App\User', 'id', 'invited_by' );
}


If i want to retrieve all invitations of current user it works,

\Auth::user()->invitations;

But if i try to get the information about the inviter it won't work! (Question: How to do it?)

\Auth::user()->invitations->inviter;

Though i can query the inviter from a invitation eloquent object like this,

\App\Invitation::first()->inviter;

But this is not working when i try to access it from the user model -> invitation -> inviter! Also can i use eager loading here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire