Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection not working in migration files in Laravel 5.2

I'm trying to use singletons for a specific class.

I did this trivially using the following in the "AppServicePrvider.php":

<?php

namespace App\Providers;

use App\Helpers\ApplicationFormHelper;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {

    }

    public function register()
    {
        $this->app->singleton(ApplicationFormHelper::class, function ($app) {
            return new ApplicationFormHelper();
        });
    }
}

I then included this class in my migration file like so:

<?php

use App\Helpers\ApplicationFormHelper;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    private $applicationFormHelper;

    public function __construct(ApplicationFormHelper $applicationFormHelper)
    {
        $this->applicationFormHelper = $applicationFormHelper;
    }

    public function up()
    {
        //...
    }

    public function down()
    {
        Schema::drop('users');
    }
}

However when I execute php artisan migrate I get the following error, indicating that dependency injection is not working.

 [Symfony\Component\Debug\Exception\FatalThrowableError]                                                             
  Type error: Argument 1 passed to CreateUsersTable::__construct() must be an instance of App\Helpers\ApplicationFor  
  mHelper, none given, called in /home/vagrant/saroia/vendor/laravel/framework/src/Illuminate/Database/Migrations/Mi  
  grator.php on line 335   

Note that I have used this class is other places (e.g. in the routes file) with no problem. It seems to be only in the migrations file that this issue exists!

like image 651
Yahya Uddin Avatar asked Jul 05 '16 23:07

Yahya Uddin


People also ask

How is dependency injection implemented in Laravel?

By default, Laravel allows you to use a completely configuration-free dependency injection system. All you need to do is type hint the class that you want to inject – for example in your controller, middleware, jobs, event listeners, etc.

How do I run a migration file in Laravel?

IF you want to re-migrate all the database, you can simply do: php artisan migrate:refresh . IF you want to make sure your database to be clean with your latest changes, you can drop your entire database tables and do php artisan migrate again. Also, you can try php artisan migrate --seed if you have any seeder.

What is unsigned in Laravel migration?

Laravel developers love the way it handles relationships between tables by just associating models with each other. The column which defines the relationship between tables must be of unsigned type.

What is migration squashing in Laravel?

Migration SquashingLaravel will write the new schema file to database/schema . Then when you run your migrations, Laravel will run the SQL from the schema file first before moving on to anything created later in the migrations folder. Note: Migration squashing is currently only supported for MySQL and Postgres.


1 Answers

As @lagbox has mentioned, migration files does not seem to be resolved by IoC containers.

However it is still possible to resolve them using the app make method like so:

<?php

use App\Helpers\ApplicationFormHelper;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
    private $applicationFormHelper;

    public function __construct()
    {
        $this->applicationFormHelper = app(ApplicationFormHelper::class);
    }

    public function up()
    {
        //...
    }

    public function down()
    {
        Schema::drop('users');
    }
}
like image 193
Yahya Uddin Avatar answered Sep 22 '22 12:09

Yahya Uddin