Download the PHP package garanaw/seedable-migrations without Composer
On this page you can find all versions of the php package garanaw/seedable-migrations. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download garanaw/seedable-migrations
More information about garanaw/seedable-migrations
Files in garanaw/seedable-migrations
Package seedable-migrations
Short Description Allows to self seed migrations in a more controlled way
License MIT
Informations about the package seedable-migrations
Laravel Seedable Migrations
About
Laravel provides a migration system with seeding. However, the seeding system is not very flexible. This package provides a more flexible way to seed your database with seed files linked to migrations.
You can run your seeding after each migration, or at the end of the batch. You can also specify seeder files that should run on the UP method or the DOWN method of your migrations.
Install
To install the package, run the following command:
After installing the package, you need to publish the migration stubs:
If you don't want to publish the stubs, you will need to extend your migrations manually from the Garanaw\SeedableMigrations\Migration
class.
The new migration file
The migration files will look a bit different now when you run the command php artisan make:migration
. The new migration files will look like this:
A few things to note here:
- The schema now is a property of the migration class. If you prefer the facade, you can still use it with
Schema::create()
. - The
up()
anddown()
now have a return typehint ofvoid
. This is not mandatory, but it is recommended. - Two new methods are added:
getTable()
andseedAt()
. ThegetTable()
method is used to get the table name of the migration. - The
seedAt()
method is used to specify when the seeder should run. You can returnSeedAt::EACH
to run the seeder after each migration, orSeedAt::BATCH
to run the seeder at the end of the batch. If you don't want to run the seeder, you can returnSeedAt::NONE
.
The new seeder file
The seeder files will look a bit different now when you run the command php artisan make:seeder
. The new seeder files will look like this:
A few things to note here:
- The
run()
method is now mandatory. This is where you should put your seeder code. - The
getData()
method is used to return the data that should be seeded. This method is mandatory now as it follows an interface. If you don't want to seed anything, you can return an empty array. - The
run()
method now has a return typehint ofbool
. This is mandatory, as it follows an interface, and it's used to store the failed seeders. - If your seeder contains a method called
configure()
, it will be called through theContainer::call()
. This means that you can use dependency injection in your seeder class to add extra configuration if needed. - Alternatively, you can create a
__construct()
method in your seeder class. This method will be called before therun()
method, and you can use dependency injection in it, but it will require the parameters from the parent class.
How to use
To run your migrations, you can use the php artisan migrate
command as usual. The seeding will be done automatically.
To specify the seeder files associated to a migration, you will need to add some methods to the migration file:
The seedersUp()
method is used to specify the seeder files that should run on the UP method of the migration. The seedersDown()
method is used to specify the seeder files that should run on the DOWN method of the migration. None of them is mandatory. If you don't specify any seeder file, the seeder will not run.
Another optional method is the shouldSeed()
method. This method is used to specify if the seeder should run or not. This method is useful if you want to run the seeder only on specific environments. The default implementation is:
Within the shouldSeed()
method, you can use the app()->environment()
method to check the environment, or any other logic to determine if the seeder should run or not.
To configure your seeder, you only need to add a configure()
method to your seeder class:
Security Vulnerabilities
If you discover a security vulnerability, please create an issue using the issue tracker.
License
The Seedable Migrations library is open-sourced software licensed under the MIT license.