Laravel service provider is no loading the Collection methods Undefined method ‘values’.intelephense(1013)

  collections, laravel, php

I want to make a Collection macro function in the service provider.
The problem is after importing the Collection class i can not access the collection funtion $this->values()
But when I use the same code in the controller is working fine.

 <?php

namespace AppProviders;

use IlluminateSupportServiceProvider;
use IlluminateSupportCollection;

class CollectionExtensions extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
      
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
    Collection::macro('transpose', function () {
            $items = array_map(function (...$items) {
                return $items;
            }, ...$this->values());
        
            return new static($items);
        });
    }
}

Source: Laravel

Leave a Reply