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!
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.
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.
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.
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.
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');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With