Relationship of Tables Laravel

1

I have a bank with the following relationships:

  

I'd like to bring the Menu relationship to the Perfil of the user.

In tiker I take the following steps:

$user = App\Models\User::find(1);
$perfil = $user->perfil;
$itemMenu = $perfil->itemMenu;

When I get the Menu, I can not

$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');
    }
}
    
asked by anonymous 01.10.2016 / 21:57

1 answer

1

I have refactored relationships by seeing that there are missing settings and relationships:

Menu

class Menu extends Model
{
    protected $primaryKey = "id_menu";
    protected $fillable = array('no_menu','ic_menu');
    protected $table = "menu";
    public $timestamps = false;

    public function itemMenu()
    {        
        return $this->hasMany('App\Models\ItemMenu', 'id_menu', 'id_menu');
    }    
}

ItemMenu

class ItemMenu extends Model
{
    protected $primaryKey = "id_item_menu";
    protected $fillable = array('id_menu','no_item_menu');
    protected $table = "item_menu";
    public $timestamps = false;

    public function menu()
    {        
        return $this->belongsTo('App\Models\Menu', 'id_menu', 'id_menu');
    }

    public function perfil()
    {        
        return $this->belongsToMany('App\Models\Perfil',
                        'perfil_item_menu', 'id_item_menu', 'id_perfil');
    }
}

Perfil

class Perfil extends Model
{
    protected $primaryKey = "id_perfil";
    protected $fillable = array('no_perfil');
    protected $table = "perfil";
    public $timestamps = false; 

    public function itemMenu()
    {        
        return $this->belongsToMany('App\Models\ItemMenu','perfil_item_menu',
                                            'id_perfil','id_item_menu');
    }    
}

User

class User extends Authenticatable implements Transformable
{
    use TransformableTrait;

    protected $fillable = ['name','email','password','id_perfil'];

    protected $hidden = ['password','remember_token'];

    public function perfil()
    {
        return $this->belongsTo('App\Models\Perfil',
                                              'id_perfil','id_perfil');
    }
}

Then to use:

$user     = App\Models\User::find(1);
$perfil   = $user->perfil();
$itemMenu = $perfil->itemMenu;
var_dump($itemMenu);
    
02.10.2016 / 00:08