相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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;

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.