sunrise
301
4
Laravel

How to write this associative query in laravel 5.2 ?

Posted 3 years ago by sunrise

How to write this associative query in laravel 5.2 ?

relationships:

    users and articles , one to many.
    categories and articles , one to many.

query:

1、I want to get the current user's articles.

2、I want to get articles' category.

    public function index()
    {
        $user=\Auth::user();
        
        //I use this sentence to query:
        $articles = $user->articles->with('category');
    
        return view('index',  compact('articles'));
    }

error: enter image description here

What should I do?

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