I would like to use Laravel 5 Models to retrieve a relationship, but i dont know which relation type i should use and how to implement it.
i have 4 database tables:
- Users
- Roles
- Permissions
- role_permission
i need to retrieve all the permissions for a "User" based on its "role_id" column.
I've created 3 models:
- User
- Role
- Permission
The database table "users" holds 2 columns:
- id
- role_id
The database table "roles" holds 2 columns
- id
- name
The database table "permissions" holds 2 columns
- id
- name
The database table "role_permission" holds 3 columns that defines which Role is associated to which Permission.
- role_id
- permission_id
- flag
What i want to achieve is the following syntax:
$user->role // Get the associated "Role"
$user->role->permissions // Get the associated permissions for a "Role"
App\Role::find(1)->permissions // Get the associated permissions for a "Role"
i did read the Laravel documentation about model relations but i really dont get it. Does someone understand what i'm trying to achieve and how to implement it in the Models? maybe with some simpel code examples so i can understand the relations and how they work?
Thanks in advance.
has_one
role. Rolesbelongs_to
user andhas_many
permissions. Permissionshas_many
roles. – Styphon