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

Your assumption is wrong. Eager loading never affected base query in any way, order clause you set there, affects only the related table query, which is posts.

To do what you want you need join.

Last updated 2 years ago.
0

@jarektkaczyk Thank you for the point in the right direction!

Correct me if I'm wrong though, but isn't using the with() method essentially doing a join within the eloquent fluent syntax? Or is that incorrect? I don't see anything in the Eloquent documentation (http://laravel.com/docs/4.2/eloquent) about a join() method.

Last updated 2 years ago.
0

So to follow up here, I was basically able to get this working with a join along these lines:

$users = User::join('posts', function($join)
{
    $join->on('users.id', '=', 'posts.user_id');
}))->orderBy('posts.created_at', 'desc')->get();

However, it looks like once you do a join query on an eloquent model, you can no longer do things like use the "with" method elsewhere. As in, in my views, I can no longer access $user->posts->property_name directly, because it's merged the 2 data results together. Is there anyway to utilize joins to for the orderBy but also keep the "with" functionality?

Last updated 2 years ago.
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.