Not really sure what you mean with the n+1 problem. Do post have other relationships that you use? If so you can get less queries by doing this
$view->with('featured', Post::with('category')->where('is_featured', 1)->take(3)->get());
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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());
}
As far as I see it you have 6 queries right now. Unless you access more relations that are not loaded using with in your view. That will bring indeed more queries. You need to select all relationships that are used in your with method to eliminate the query count!
Please or to participate in this conversation.