Laravel - parameters for passing through app-> binding to the model constructor

Well, the code describes it all. I have an Entity service provider that passes an instance of a playlist model that should receive an array as the first parameter to the constructor. How to pass this parameter through app-> bind? Knowing that EntityServiceProvider is automatically entered when specified in the controller.

        // Current Code
        /**
         * Playlist Entity
         *
         * @return Playlist\PlaylistEntity
         */
        $this->app->bind('Playlist\PlaylistEntity', function($app)
        {
            return new PlaylistEntity($app->make('Playlist\PlaylistRepositoryInterface'));
        });



        // Should be something like this:
        /**
         * Playlist Entity
         *
         * @return Playlist\PlaylistEntity
         */
        $this->app->bind('Playlist\PlaylistEntity', function($app)
        {
            return new PlaylistEntity($app->make('Playlist\PlaylistRepositoryInterface', $parameters));
        });

Similar case: Laravel 4: transferring data from make to the service provider

+6
source share
3 answers

Comment by Alex Russell also works for me.

" " , . $this->app->bind('Whatever', function ($app, $params) { var_dump($params); });, App::make('Whatever', [1, 2, 3]); var_dumps [1, 2, 3] .

+6

Laravel 5.4 App:: make(), :: makeWith().

, , . , - ?

+4

@yevgeniy-afanasyev , . , : https://github.com/laravel/ideas/issues/391#issuecomment-285197048

, . , ::make.

// Service Provider
$this->app->bind(MyClass::class, function ($app) {
   return function($param) : MyClass
   {
       return new MyClass($param);
   }
}

// ::make
$myInstance = App::make(MyClass::class)($myParameter);

// mock
$myMock = Mockery::mock(new MyClass($myParameter));
$this->instance(MyClass::class, function($param) use ($myMock) { return $myMock; });
0
source

Source: https://habr.com/ru/post/1526682/


All Articles