Cannot run raw Query in Laravel 4
laravel, laravel-4
Solution
As mentioned by other contributors;-
DB::select('SQL QUERY GOES HERE WITH PARAMETERS ?, ?', array('parameter 1', 'parameter 2'));
The code above should permit raw sql.
However,
DB::table('table1')
->select((DB::raw('MAX(score)')))
->where('status','=', 1)
->groupBy('user_id')
->count();
Should achieve the same effect for the task you seek it for, and is more in tune with laravels philosophy.
Problem
I need to get total count to show in my page. I can run this & loop through & get the total number ``` DB::table('table1') ->select((DB::raw('MAX(score)'))) ->where('status', 1) ->groupBy('user_id') ->get(); ``` But this query would give me the count in just a single query & I don't need run any extra loop to get the total. ``` SELECT COUNT( * ) FROM ( SELECT MAX( score ) FROM table1 WHERE status =1 GROUP BY user_id ) AS totalCounter ``` How am I suppose to run this RAW query in Laravel 4?