selectRaw takes only 2 arguments. The first is the query itself and the second are any parameters
selectRaw('price * ? as price_with_tax', [2]);
So in your case you can do this instead
selectRaw('usuario_ad, count(usuario_ad) as Conteo')
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
What's up friends.
Well, I'm building a query to query builder, I managed to structure it but I have an error that I do not know how to solve.
$data = DB::table('usuarios')
->selectRaw('usuario_ad', 'count(usuario_ad) as Conteo')
->whereIn('usuario_ad', function($query){
$query->select('username')
->from('users')
->where('users.refer', 'Thor')
->union('Thor');
})->groupBy('usuario_ad')->paginate(20);
the error.
"Type error: Argument 2 passed to Illuminate\Database\Query\Builder::selectRaw() must be of the type array, string given, called in C:\laragon\www\arreglo\app\Http\Controllers\CuentaController.php on line 46 ◀"
Use
$users = DB::table('users')->where('refer', 'Thor')->select('username')->get()->push('Thor');
and then
$data = DB::table('usuarios')
->selectRaw('usuario_ad, count(usuario_ad) as Conteo')
->whereIn('usuario_ad', $users)
->groupBy('usuario_ad')
->paginate(20);
Please or to participate in this conversation.