The scene is: I have a user model, a Role model and a Permission model with Many to Many and one to many relationships.
User model (you can see role one to many relationship, permission many to many relationship):
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
// ------------------------------------------------------------------------------
// Model Traits
// ------------------------------------------------------------------------------
use Notifiable;
// ------------------------------------------------------------------------------
// Model configuration (extended from parent)
// ------------------------------------------------------------------------------
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token'
];
/**
* The attributes that should be eager loaded.
*
* @var array
*/
protected $with = ['role', 'permissions'];
// ------------------------------------------------------------------------------
// Model Relationships
// ------------------------------------------------------------------------------
public function role() {
return $this->belongsTo('App\Models\Role');
}
public function permissions() {
return $this->belongsToMany('App\Models\Permission');
}
}
Role model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
class Role extends Model
{
use Cachable;
protected $with = ['permissions'];
// ------------------------------------------------------------------------------
// Model Relationships
// ------------------------------------------------------------------------------
public function users() {
return $this->hasMany('App\Models\User');
}
public function permissions() {
return $this->belongsToMany('App\Models\Permission');
}
}
Permission model:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
class Permission extends Model
{
use Cachable;
// ------------------------------------------------------------------------------
// Model configuration (extended from parent)
// ------------------------------------------------------------------------------
/**
* Campos autorellenables para la generación automática de modelos
*/
protected $fillable = ['name'];
// ------------------------------------------------------------------------------
// Model Relationships
// ------------------------------------------------------------------------------
public function users() {
return $this->belongsToMany('App\Models\User');
}
public function roles() {
return $this->belongsToMany('App\Models\Permission');
}
}
If I call User::with(['role', 'permissions'])->get()
or even User::with(['role.permissions', 'permissions'])->get()
I get the user data, the role data and permission data. Everything is OK, except, the role permissions are never loaded. So I cannot work with them without adding another query for this user role permission.
I know that the role have permissions 'cause when I execute Role::where('id', 3)->with('permissions')->get();
the relationship works and has the permission data.
What I am doing wrong?
My DB tables are: users, permissions, roles, permission_role and permission_user.
Edit - This is the result I get from the statement:
[
{
"id": 1,
"role_id": 3,
"name": "UserName",
"email": "[email protected]",
"email_verified_at": null,
"created_at": "2018-12-10 13:04:00",
"updated_at": "2018-12-10 13:04:00",
"role": {
"id": 3,
"name": "Editor",
"created_at": "2018-12-10 13:03:31",
"updated_at": "2018-12-10 13:03:31",
"permissions": []
},
"permissions": [
{
"id": 3,
"name": "Delete post",
"created_at": "2017-10-23 11:07:43",
"updated_at": "2017-10-23 11:07:43",
"pivot": {
"user_id": 1,
"permission_id": 3
}
}
]
}
]
As you can see, permissions of the role are empty.