Download the PHP package mylesduncanking/laravel-simple-migration without Composer
On this page you can find all versions of the php package mylesduncanking/laravel-simple-migration. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mylesduncanking/laravel-simple-migration
More information about mylesduncanking/laravel-simple-migration
Files in mylesduncanking/laravel-simple-migration
Package laravel-simple-migration
Short Description Streamlined class for simple migrations within Laravel
License MIT
Homepage https://github.com/mylesduncanking
Informations about the package laravel-simple-migration
Install via composer
composer require mylesduncanking/laravel-simple-migration
Getting started
To use this an understanding of how Laravel's migrations work is required. "Migrations are like version control for your database, allowing your team to define and share the application's database schema definition. If you have ever had to tell a teammate to manually add a column to their local database schema after pulling in your changes from source control, you've faced the problem that database migrations solve." - Laravel documentation
To use simple migrations, create a new migration file using the same syntax as a default Laravel artisan migration but specify that you would like a simple-migration
. For example php artisan make:simple-migration your_migration_name
Within the migration file you will see a new protected array
property called $migration
which is where you will define your migration logic.
The format of the $migration
array is as follows:
Auto-after functionality
To save some time when adding multiple columns to a table, the default behaviour is changed to add columns sequentially. This removes the requirement of adding ->after('foobar')
to every column modifier.
You can disable this behaviour by running php artisan vendor:publish --tag=simplemigration
and changing config/simplemigration.php > auto_after
to false
Auto-index functionality
To save some time you can use the automatic index feature. By default this will automatically add the ->index()
modifier to any column ending in _id
You can modify these rules by running php artisan vendor:publish --tag=simplemigration
and changing the values within the config/simplemigration.php > auto_index
array. Note: These values are in a regex format.
You can also specify an auto-index column to not be indexed ad-hoc by passing the noIndex
option in the modifiers array.
Table naming convention
Within the $migration
property create a key for each table you want to migrate. Within each sub array is where you define the column changes.
If an id
or uuid
column is defined within the column set then the table will be created, otherwise it will be updated. You can overwrite this by prefixing the table name with either create:
or update:
depending on the method you would like to force.
You can modify these assumptions by running php artisan vendor:publish --tag=simplemigration
and editing config/simplemigration.php > type_assumptions
For example:
How to format column keys
The column is passed as the key within the table array.
The format should be defined as { Type }:{ Column name }. For example integer:quantity
If you want to pass additional parameters to the type method you can seperate these by a comma. If an array is required seperate values with a pipe |
. For example set:eye_color,blue|green|brown|other
If you don't pass a type then an assumption will be made as to what type should be used.
Assumed type | |
---|---|
Ends in _id |
foreignId |
Ends in _at |
timestamp |
Anything else | string |
You can modify these assumptions by running php artisan vendor:publish --tag=simplemigration
and editing config/simplemigration.php > create_triggers
. Note: This is in a regex format.
More information on valid Laravel column types can be found in Laravel's documentation.
How to format column modifiers
As the value of each column you pass an array. This array can either be empty or define modifiers to the column.
Each value in the array should follow the format of { Modifier }:{ Parameters }. For example after:id
More information on valid Laravel column modifiers can be found in Laravel's documentation.
Example migration
The following migration creates a roles table and add a foreign key into the users table.
All versions of laravel-simple-migration with dependencies
illuminate/database Version ^7.0|^8.0|^9.0|^10.0|^11.0
illuminate/support Version ^7.0|^8.0|^9.0|^10.0|^11.0