php - Laravel eloquent, how to order custom sortby -
i have following code works fine:
$products = product::like($search)->wherein('id', $request->input('product_ids'))->skip($offset)->take($limit)->get(array('products.*'))->sortby(function($product) use ($sort_order) { $number = (isset($sort_order[$product->id])) ? $sort_order[$product->id] : 0; return $number; });
this returns items in ascending order, how specify whether want sortby return products in ascending or descending order?
//$order contains either 'asc' or 'desc' $products = product::like($search)->wherein('id', $request->input('product_ids'))->skip($offset)->take($limit)->get(array('products.*'))->sortby(function($product) use ($sort_order, $direction) { $number = (isset($sort_order[$product->id])) ? $sort_order[$product->id] : 0; return ($direction == 'asc') ? $number : -$number; });
Comments
Post a Comment