I created an appends attribute in Laravel Model, from the code below.
protected $appends = array(\'total\'=>\'\');
And I set the returne
As mentioned, the proposed solution using a sortBy()
callback does not work with paging.
Here is an alternative to the accepted solution:
If the appended attributes can be calculated from the fields in your query you can instead use orderByRaw()
, like so:
// For example, booking percentage
$query->orderByRaw('bookings_count / total_count');
Check out a more advanced example here: laravel orderByRaw() on the query builder