1
I have a bank with the following relationships:
I’d like to bring the relationship of Menu
as per the Perfil
of the user.
In tiker I do the following steps:
$user = App\Models\User::find(1);
$perfil = $user->perfil;
$itemMenu = $perfil->itemMenu;
When I get the menu, I can’t
$menu = $itemMenu->menu;
In my class User.php
class User extends Authenticatable implements Transformable
{
use TransformableTrait;
/**
* 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'
];
public function perfil()
{
return $this->belongsTo('App\Models\Perfil','id_perfil');
}
}
In class Perfil
class Perfil extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'perfil';
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id_perfil';
protected $fillable = [
'id_perfil',
'no_perfil'
];
public function itemMenu()
{
return $this->belongsToMany('App\Models\ItemMenu', 'perfil_item_menu', 'id_perfil', 'id_item_menu');
}
}
Class Menu
class Menu extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'menu';
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id_menu';
protected $fillable = [
'id_menu',
'no_menu',
'ic_menu'
];
public function itemMenu()
{
return $this->hasMany('App\Models\ItemMenu', 'id_menu');
}
public function perfil()
{
return $this->belongsToMany('App\Models\Perfil', 'perfil_menu', 'id_menu', 'id_perfil');
}
}
Class ItemMenu
class ItemMenu extends Model implements Transformable
{
use TransformableTrait;
protected $table = 'item_menu';
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id_item_menu';
protected $fillable = [
'id_item_menu',
'id_menu',
'no_item_menu'
];
public function menu()
{
return $this->belongsTo('App\Models\Menu', 'id_menu');
}
public function perfil()
{
return $this->belongsToMany('App\Models\Perfil', 'perfil_item_menu', 'id_item_menu', 'id_perfil');
}
}
in User what is the name of the field that relates to profile, in your model is not described.
– novic
I made a reply, when do interaction check if it is ok or some error let me know!
– novic