OrderBy on whereHas request in Laravel 4.2

How can I sort the returned data from a query using whereHas ? In my query, I have to get the user data that exists in the table of interest , but I need to sort it using the datetime column from interest . But the return request does not sort the data at all.

Here are my code snippets if this helps you understand my problem.

Model (name: user)

//***relationship***// public function interest(){ return $this->belongsTo('Interest','id','interest_by'); } 

controller

 $userInterested = User::whereHas('interest',function($q) use ($id) { return $q->where('interest_on', $id) ->orderBy('datetime'); }); $userQuery = $userInterested->get(); return $userQuery; 
+5
source share
2 answers

remove return from where has and try this.like

 $userInterested = User::whereHas('interest',function($q) use ($id) { $q->where('interest_on', $id) ->orderBy('datetime'); })->get(); return $userInterested; 
0
source
 $userInterested = User::whereHas('interest',function($q) use ($id) { return $q->where('interest_on', $id); })->orderBy('datetime'); $userQuery = $userInterested->get(); return $userQuery; 
0
source

All Articles