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

mleontenko's avatar

Raw SQL query to array

I'm using a raw SQL query that selects some ids from database. It looks like this:

$query = DB::select('SELECT pr.id_obrasca_prethodne 
        FROM public."prethodneNacionalneProcjene" AS pr 
        JOIN public."obrasciProcjeneVrste" AS ob ON pr.id_obrasca = ob.id WHERE ob.status LIKE \'Prihvaćen\'');

If I output the $query variable now, I get:

[{"id_obrasca_prethodne":8},{"id_obrasca_prethodne":28}]

I need to convert the result of query to PHP array so it contains only numbers (8, 28, ...). How can I do this?

0 likes
5 replies
Sinnbeck's avatar
$ids = $query->pluck('id_obrasca_prethodne');
mleontenko's avatar

@sinnbeck I'm getting the following error:

Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function pluck() on array
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

then just add collect first

$ids = collect($query)->pluck('id_obrasca_prethodne');

Please or to participate in this conversation.