rin4ik
364
12
Eloquent

How can I get rid of n+1 problem

Posted 1 year ago by rin4ik

I'm using class based composers and don't know how to solve n+1 problem

  public function compose(View $view)
    {
        $view->with('categories', Category::with('posts')->orderBy('title', 'asc')->get());

        $view->with('tags', Tag::all());

        $view->with('popular', Post::orderBy('views', 'desc')->take(3)->get());

        $view->with('featured', Post::where('is_featured', 1)->take(3)->get());

        $view->with('recent', Post::orderBy('date', 'desc')->take(3)->get());
    }

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

Reply to

Use Markdown with GitHub-flavored code blocks.