Support the ongoing development of Laravel.io →
Database Eloquent

I'm having difficulty to let the laravel query order by a date field of a nested query. This is what I came up with

$twitterStatuses = ProjectTwitterStatus::with(array('twitterStatus' => function($query) {
			$query->select('id','datetime','text','twitter_user_id','raw_created_at','datetime')
				  ->with(array('twitterUser' => function($query) {
						$query->select('id','id_str','screen_name','profile_image_url','screen_name','name','description');
				  }))
				  ->with(array('twitterStatusTag' => function($query) {
						$query->with('tag');
				  }))
				  ->with(array('twitterUserTag' => function($query) {
						$query->with('tag');
				  }));
			}))
		->orderBy('datetime')
		->paginate(15);

ProjectTwitterStatus holds only a projectId and the twitterStatusId, the last one identifies the tweet in twitterStatus. The result needs to be ordered by the datetime of the tweet, and that is availible in the twitterStatus model. The above depicts what I thought it shoud be, but that did not work, any thoughts?

Last updated 3 years ago.
0

You should be using orderBy on the nested relation which you want to inherit order precedence from not on the root.

Last updated 3 years ago.
0

I tried that, but that results in an extreamly wierd ordering, the ordering restarts more than once.

Last updated 3 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.

© 2025 Laravel.io - All rights reserved.