arakibi
arakibi

Reputation: 447

running a mysql query containing division in laravel 5

I want to run this mysql query in Laravel 5 using the DB query :

// SELECT *, rating/number as total FROM `courses` order by total DESC; 

This is what I tried :

$query   = \DB::table('courses')->select('*');
$courses = $query->addSelect('rating/number as total')
                 ->orderBY('total DESC')
                 ->get();

but, rating/number is considered as a table column . The same thing happens when I tried it inside parenthesis (rating/number). Any help?

Upvotes: 0

Views: 3049

Answers (2)

Code Commander
Code Commander

Reputation: 17300

Can you use Raw Expressions for it? Maybe something like this:

$courses = \DB::table('courses')
                 ->select(DB::raw('*, (rating / number) as total'))
                 ->orderBy('total DESC')
                 ->get();

Upvotes: 0

Jarek Tkaczyk
Jarek Tkaczyk

Reputation: 81187

$courses = \DB::table('courses')
              ->selectRaw('*, rating/number as total')
              ->orderBY('total', 'DESC')
              ->get();

Upvotes: 2

Related Questions