galih56
316
9
Laravel

How to join table using eloquent?

Posted 4 months ago by galih56

I want to join 2 tables.Users and Role. The table structure is Role table have many user but user only have 1 role i'm still confused how to display name of the role from the role table.

Model User

public function role()
    {
        return $this->belongsTo('App\Role');
    }

Model Role

  public function user()
    {
        return $this->hasMany('App\User'); //pake local_key kalo nama id usernya bukan ' id'
    }

in a page.blade.php i call Auth::user()->role()->nama

what am i wrong?? can someone correct me??

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