I would like to use orderBy in laravel but its not working while I have used CAST there
$this->adLists = ListingVisitor::with('listing')->selectRaw('CAST(sum(amount) as UNSIGNED) as total,listing_id,amount')->whereHas('listing.user',function($q){
$q->where('id',Auth::user()->id);
})->groupBy('listing_id')->orderBy('total','DESC')->paginate(10);
The result would be
136, 66, 1 ,5
Its actually should be 136, 66, 5, 1
I have using CAST in selectRaw but it has no difference
2
Answers
I think there is no requirement to cast it. When you do sum it already return you with the numeric format. Instead try to Order with the SUM function. It will give you the expected output.
You can use DB::raw, This will solve any issue with ordering by the aliased column when using CAST in laravel