Search code examples
laravelmodel-view-controllereloquent

Does anyone know how to order data with 2 different parameter types?


Does anyone know how to order data with 2 different parameter types?

example :

I want products that have a halal label to appear first but only products that have more than 40 in stock then display the products with the most stock first to the smallest stock

This is the best thing I can do, but it doesn't seem right yet.

$data = $data->havingRaw("(halal == 1 and product>40)")->orderBy('product','desc');

Solution

  • Simply invoke order by as many time as you want:

    $data = $data
        ->havingRaw("(halal == 1 and product>40)")
        ->orderBy('product', 'DESC')
        ->orderBy('id', 'ASC')
        ->get();