Search code examples
laravellaravel-5laravel-5.3

How to get access to relationship in model User Laravel?


I have default User model:

class User extends Authenticatable implements HasRoleContract
{
    use Notifiable, HasRole;

}

With one relationship inside:

public function distributor() {

    return $this->hasOne('App\DistributorContacts', 'distributor_id', 'id');
}

So, when user passed authorization I can not see this relation in object:

{{dd(Auth::user())}}

enter image description here


Solution

  • you may use ->with('distributor') on your user Object to get relationships loaded.

    e.g.

    $user = new User()->with('distributor');
    dd($user->distributor);
    

    or

    Auth::user()->with('distributor');