Pivot table not getting relationship

Posted 1 month ago by pickab00

I have two tables. One is users and the other is menus. They have a table menu_user which is the pivot table and here is the relationship.

class Menu extends Model
{

        use SoftDeletes;

        public function users(){
        return $this->belongsToMany('App\User');
    }

}

class User extends Model
{

        use SoftDeletes;

        public function menus(){
        return $this->belongsToMany('App\Menu');
    }

}

And this is what I am trying in my controller:

$test = Auth::user()->with(['menus' => function($q){
        $q->with('menuroutes');
    }])->get();
//menuroutes is related to the menu table. menu routes belongs to menu and menu has many menuroutes.

    foreach ($test->menus as $menus){
        return $menus;
    }

    return $test;

Basically I want to take the menus belonging to the auth user but it is giving me this error.

"Property [menus] does not exist on this collection instance."

Thanks

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.