Relationship of Laravel Tables

Asked

Viewed 1,184 times

1

I have a bank with the following relationships:

inserir a descrição da imagem aqui

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.

  • I made a reply, when do interaction check if it is ok or some error let me know!

1 answer

1


I remade the relationships, by seeing that 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);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.