Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

daniel21gt's avatar

Consultation construction Query builder, laravel 5.5

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 "

0 likes
10 replies
bobbybouwmann's avatar

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')
1 like
Snapey's avatar

try

->selectRaw('usuario_ad, count(usuario_ad) as Conteo')
daniel21gt's avatar

Thank you very much, that mistake does not come out, now I get this one.

"Call to a member function getBindings() on string"

Snapey's avatar

Can you explain your whereIn query?

You have a users table and a usuarios table ?

daniel21gt's avatar

If snapey, are two tables, apologize for ambiguity, the users table is a table for users with role, while the usuarios table is for users without a role. whereIn is to consult from the usuarios table, all people that are directly related to 'thor', in table users.

This is the original query in sql that works for me.

select usuario_ad, count(usuario_ad) from usuarios where usuario_ad in (select username from users where users.refer="Thor" union select "Thor") group by (usuario_ad);

daniel21gt's avatar

The last mistake I mentioned, points to this line, $this->addBinding($query->getBindings(), 'union');

public function union($query, $all = false)

{

    if ($query instanceof Closure) {

        call_user_func($query, $query = $this->newQuery());
    }

    $this->unions[] = compact('query', 'all');

    $this->addBinding($query->getBindings(), 'union');

    return $this;
}
Snapey's avatar

so your subquery returns an array of usuarios names AND 'Thor' ?

better to write that as its own query and then use as array in whereIn

write and test that bit on its own?

daniel21gt's avatar

Exact, subquery returns an array of usuarios names AND 'Thor', I will try to fix it.

Snapey's avatar
Snapey
Best Answer
Level 122

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);
daniel21gt's avatar

Thanks Snapey for your time, run the lines, this error appears.

"Object of class stdClass could not be converted to string"

Should I pass the array of objects to string?

Please or to participate in this conversation.