tjyoung
asked on
Trying to create a query but having trouble comparing 2 columns in php laravel
Hi,
I have a list of vehicles in a mysql db and I'm building a query within my controller using laravel framework.
My problem is I have 2 columns (vehicle_price and discount_price)
I'm trying to return my result order by lowest price on up.
Because some values exist in the discount_price column I can't figure out how to return a list with the lowest prices ascending taking both columns into account.
You'd think perhaps I could test if there is a value in the discount_price, use that but no idea what to do.
fyi: when the discount_price column is 'empty' its a 0 (zero)
I have a list of vehicles in a mysql db and I'm building a query within my controller using laravel framework.
My problem is I have 2 columns (vehicle_price and discount_price)
I'm trying to return my result order by lowest price on up.
Because some values exist in the discount_price column I can't figure out how to return a list with the lowest prices ascending taking both columns into account.
You'd think perhaps I could test if there is a value in the discount_price, use that but no idea what to do.
fyi: when the discount_price column is 'empty' its a 0 (zero)
public function showUsed()
{
$vehicles = DB::table('used_vehicles')
->join('dealerships', 'used_vehicles.dealer_id', '=', 'dealerships.id')
->join('makes', 'used_vehicles.make_id', '=', 'makes.id')
->join('models', 'used_vehicles.model_id', '=', 'models.id')
->join('used_vehicles_categories', 'used_vehicles.category_id', '=', 'used_vehicles_categories.id')
->select('used_vehicles.*','dealerships.name','dealerships.address_street','dealerships.address_city','dealerships.address_province','dealerships.address_postal_code','dealerships.phone','dealerships.toll_free_phone','makes.name as make_name','models.model_name as model_name','used_vehicles_categories.category_title as category_name')
->orderBy('used_vehicles.vehicle_price', 'asc')
->paginate(30);
return view('/pages/used/index', ['vehicles' => $vehicles]);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Glad to help! Best regards, ~Ray
ASKER
Great suggestion.