Laravel - pass parameters through app->bind to model's constructor

Omranic picture Omranic · Feb 13, 2014 · Viewed 11.6k times · Source

Well, code describes it all. I've an Entity Service Provider, which pass an instance of Playlist Model, which supposed to get an array as it's first constructor parameter. How to pass that parameter through app->bind? Knowing that EntityServiceProvider is automagically injected when referenced 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: Passing data from make to the service provider

Answer

Dustin Graham picture Dustin Graham · Apr 5, 2016

Alex Russell's comment works for me as well.

The answer in the 'similar case' post is correct as far as I can tell. $this->app->bind('Whatever', function ($app, $params) { var_dump($params); }); followed by App::make('Whatever', [1, 2, 3]); var_dumps the [1, 2, 3] array for me.