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;
source share