Error with relationship - Laravel

0

I have 2 related tables and I want to show 1 data that has only one "pulling" this relationship.

tables:

Users:

Permissions:

Tablethatwilllist:

Code:

@foreach(App\User::all()as$user)<tr><td>{{$user->id}}</td><td>{{$user->name}}</td><td>{{$user->permission->name}}</td><td>{{$user->email}}</td></tr>@endforeach

Model"User":

public function permission()
{
    return $this->hasOne('App\Permission', 'id', 'role');
}

error:

    
asked by anonymous 27.07.2018 / 20:27

1 answer

0
The second term of the belongsTo () function is the name of the key you have in the users table that will serve as a comparison with the permissions.

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

Automatically it will compare with the id column of the permissions table, unless you define in the third term which column should serve as a comparison.

    
27.07.2018 / 20:42