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

Chitrasen's avatar

Convert multiple array into a string in laravel

$est_data = Establishments::where('status',0)->where('city','=',$data['location'])->get(array('id')); return $est_data;

result [{"id":43},{"id":71},{"id":41},{"id":39}]

This is my above laravel condition and result, i want the result to be as like 43,71,41,39. Can anyone please help me, how can be this done using php. i tried with implode function, but getting error, please help...thank you

0 likes
6 replies
tisuchi's avatar

Have you tried pluck() @Chitrasen ?

For example-

return Establishments::where('status',0)->where('city','=',$data['location'])->pluck('id'); 
4 likes
Chitrasen's avatar

yes tisuchi i know , but using pluck i am getting only first value, but not getting all the values

vajid's avatar

try get with pluck

$test_data =  Establishments::where('status',0)
                        ->where('city','=',$data['location'])
                        ->get()
                        ->pluck(['id'])
                        ->toArray(); 

remove toArray() if you want collection object

Chitrasen's avatar

Sorry its showing Call to undefined method error

vajid's avatar

can you post screen of your error page

burlresearch's avatar

If you want a string, then use implode, if you need an array, then stop at pluck:

Establishments::where('status', 0)
    ->where('city', '=', $data['location'])
    ->pluck(['id'])
    ->implode(',');
// > "43,71,41,39"

Establishments::where('status', 0)
    ->where('city', '=', $data['location'])
    ->pluck(['id']);
// > [43, 71, 41, 39]

Please or to participate in this conversation.