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
.
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!
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community