I have a question regarding a MySQL query.
I would like to know how to create the same query using Laravel QUERY Builder
SELECT count( * ) as total_record FROM `player_games` WHERE team_score < rival_score
Thanks
I have a question regarding a MySQL query.
I would like to know how to create the same query using Laravel QUERY Builder
SELECT count( * ) as total_record FROM `player_games` WHERE team_score < rival_score
Thanks
try this one
$query = "SELECT count( * ) as total_record FROM `player_games` WHERE team_score < rival_score";
$count = \DB::select(\DB::raw($query));
secod way
DB::table('player_games')->where('team_score','<','rival_score')->count();
in Laravel Query Builder you can write this:
$player_games = DB::table('player_games')->where('team_score','<', 'rival_score')->count();
Reference: https://laravel.com/docs/5.7/queries
There's a subtle thing here that you need to be aware of:
DB::table('player_games')
->where('team_score','<',\DB::raw('`rival_score`'))
->count();
The reason why you need \DB::raw is because if you don't then the right-hand side of the where will be automatically assumed to be a value and be passed as a binding, however you need to pass it as a raw DB expression to indicate that it's actually a column name. The backticks are added because it's good to escape the column names.
$query->whereColumn('left_column', '<', 'right_column')