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



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