Did you know that 1 in 4 visitors would abandon a website if it takes more than 4 seconds to load? While multiple factors can slow down a website, one common factor is inefficient database queries.
Our controller would look something like this: public function index() { return view('products', [ 'products' => Products::query()->paginate()]); }
To use eager loading, you call the with method in your query public function index() { return view('products', [ 'products' => Products::query() ->select(['id', 'title', 'slug', 'thumbnail']) ->with('brand') ->paginate()]); }
We can do this by adding a.category to the load method call: public function show(Category $category) { $category->load('products.category'); // eager load the products return view('categories.show', ['category' => $category]); }