Cant use toArray after Filter and Pluck a collection laravel

I need to display a list of category label from a filtered collection seperated by comma in blade views.

The code looks like this

{{$data->categories->filter(function ($value) {return $value->type_id == 1;})->pluck(['label'])->toArray()}};

This error is when I use toArray()

htmlspecialchars() expects parameter 1 to be string, array given

I need the result in array to use it with implode to display the list seperated by comma

{{implode(', ',$data->categories->filter(function ($value) {return $value->type_id == 1;})->pluck(['label'])->toArray())}};

Expected result to display:

Car, Truck, Cheap

The collection

[{"id":80,"type_id":1,"label":"Car","pivot":{"data_id":6060,"category_id":80}},
{"id":45,"type_id":1,"label":"Truck","pivot":{"data_id":6060,"category_id":45}},

{"id":75,"type_id":8,"label":"Cheap","pivot":{"data_id":6060,"category_id":75}}]

If I dont use filter / where the collection, I can use toArray()

Any idea why using filter/where collection causing toArray() error?

Source: Laravel StackOverflow

Leave a Reply