I have a project that i want to add a filter functionality to it using meilisearch. And what I'm trying to do is creating a new macro collection,
// App\Providers\AppServiceProvider.php
public function boot(): void
{
Collection::macro('recursive', function () {
$this->map(function ($value) {
if (is_null($value)) return;
if (is_array($value) || is_object($value)) {
return collect($value)->recursive();
}
return $value;
});
});
}
And when I'm trying to call the macro to filter,
// dd($this->queryFilters)
/*
array:2 [▼ // app\Http\Livewire\ProductBrowser.php:26
"color" => []
"size" => []
]
*/
$filters = collect($this->queryFilters)
->filter(fn ($filter) => !empty($filter))
->recursive()
->map(function ($value, $key) {
return $value->map(fn ($value) => $key . ' = "' . $value . '"');
})
->flatten()
->join('AND');
dd($filters);
It gives me Error:Call to a member function map() on null
it seems to be the "recursive()" function that returns null.
I tried to add the recursive macro to the collection.
I think the problem is in the "recursive()" and maybe in meilisearch version I don't really know.
Can you update your marcro
like following "just to ensure you imported the Collection
\Illuminate\Support\Collection::macro('recursive', function () {
return $this->map(function ($value) {
if (is_array($value) || is_object($value)) {
return collect($value)->recursive();
}
return $value;
});
});