Support the ongoing development of Laravel.io →
Requests Database Testing
Last updated 2 years ago.
0
Solution

You could set up One To Many Relationship in your Article and Category classes. Then you'll be able to just write:

$articles = $article->latest()->published()->with('category')->simplePaginate(10);

It will be two queries, admittedly, but at least not N+1.

As for the actual error, id field used in ORDER BY clause is ambiguous (i.e. it could refer to article.id or category.id). If it's part of your latest() method, change it to use article.id instead of just id.

Last updated 9 years ago.
0

Oh wow, this is awesome!

I changed:

$articles = $article->latest()->published()->simplePaginate(10);

To:

$articles = $article->latest()->published()->with('category')->with('user')->with('tags')->simplePaginate(10);

My SQL queries before was 19 now it's 8! Thank you very much!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.