Laravel 5.4 Concrete table migration
Hi everyone, including the documentation at https://laravel.com/docs/5.4/migrations .
Is there a way to migrate a specific migration file (for 1 migration only), call right now every time there is a change. I use php artisan migrate:refresh
and all fields are reset.
source to share
You must first create one file migration
for your table, for example:
public function up()
{
Schema::create('test', function (Blueprint $table) {
$table->increments('id');
$table->string('fname',255);
$table->string('lname',255);
$table->rememberToken();
$table->timestamps();
});
}
After creating a test folder in the migrations folder , then the newly created migration is moved / copied to the test folder and run under below command in your terminal / cmd like:
php artisan migrate --path=/database/migrations/test/
source to share
Look at the table migrations
in your database, a list of the migration file name and batch number will appear.
Suppose you have the following structure,
id migration batch
1 2014_10_12_000000_create_users_table 1
2 2014_10_12_100000_create_password_resets_table 1
3 2016_09_07_103432_create_tabel_roles 1
If you just want to 2016_09_07_103432_create_tabel_roles
migrate 2016_09_07_103432_create_tabel_roles
, change its migration value to 2, which is the highest of all, and then just do the following.
php artisan migrate:rollback
Only the table with batch value 2 will be rolled back here. Now modify this table and run the following console command.
php artisan migrate
The batch value in the table migrations
determines the order of migration. when you roll back migrations that are the last or most significant of the batch are carried over first, and then others. This way you can change the value in the database and then drop a specific migration file.
Although it is not always recommended to change the batch number every time due to the relationship between the table structure, we can use this case for some cases where rolling back one table does not violate the integrity of the tables.
I hope you understand.
source to share
Just wanted to post another solution that I think is worth mentioning.
- Find the line with your migration name in the migrations table and DELETE it. It should look like this: 2016_06_01_000001_create_oauth_auth_codes_table
- Drop your table from the database, e.g. DROP TABLE oauth_auth_codes
- Run php artisan migrate
It will only move the table you need and will not touch anything else
source to share
You can only rollback:
php artisan migrate:rollback
https://laravel.com/docs/5.4/migrations#rolling-back-migrations
You can specify how many migrations need to be done to revert to using the "step" option:
php artisan migrate:rollback --step=1
Some tricks are available here:
source to share
php artisan help migrate
You will see an option:
--path [= PATH] Path to migration files to be executed
By the way, you can specify the root folder of the file you want to transfer:
php artisan migrate --path=/database/migrations/sample.php
Or you can create a new folder in migrations and then move all the migration files you want inside it:
php artisan migrate --path=/database/migrations/new_folder
source to share
You need to put the file in a new directory (ex: selected) and then apply
php artisan migrate --path=/database/migrations/selected
if you need a rollback:
php artisan migrate:rollback --path=/database/migrations/selected
Notes:
php artisan migrate:refresh
this is rolling back and then migrating all migration files in the default directory (/ database / migrations)
source to share
If you want to create another table, just create a new migration file. It will work.
If you create a transfer named users_table
with id, first_name, last_name
. You can create a migration file like
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name',255);
$table->string('last_name',255);
$table->rememberToken();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('users');
}
If you want to add another filed as "status" without migration: update. You can create another migration file like "add_status_filed_to_users_table"
public function up()
{
Schema::table('users', function($table) {
$table->integer('status');
});
}
And don't forget to add the rollback option:
public function down()
{
Schema::table('users', function($table) {
$table->dropColumn('status');
});
}
And when you run migrate with php artitsan migration
, it just migrates the new migration file.
But if you add "status" to the first mgration file (users_table) and run the migration. Nothing to migrate. You need to run php artisan migrate:refresh
.
Hope for this help.
source to share
You can use this.
→ https://packagist.org/packages/sayeed/custom-migrate
→ https://github.com/nilpahar/custom-migration/
it is very easy to use
source to share