How can I migrate my new migration file in Laravel? - laravel

sorry for silly question but I'm really facing problem. After migrating all the migration files. When I need another features for my project I have to make another table but When I'm going to migrate the new table using "php artisan migrate:rollback" or "php artisan migrate:rollback step=17" .... all the migration files are migrating this time too and I am losing all previous data. Then How can I migrate only the new one?

To migrate only new migrations is simple: php artisan migrate. The way the process works is that Laravel creates a table in your database called migrations. In that table are the names of the migrations that have been run already. If there are new migrations the above command will work.
You can read more about running migrations in the documentation.

To migrate any new migration created php artisan migrate is the artisan command.
To get the details of all commands php artisan is the artisan command.
To get the details of any specific command php artisan help command-name-here.

In the case of creating a new file, you only need to run php artisan migrate again.
If you want modify original migration file and migrate it again, you can use this package to migrate.
First, install package in your Laravel project:
composer require caloskao/migrate-specific
Register command at app/Console/Kernel.php :
protected $commands = [
\CalosKao\MigrateSpecific::class
];
Now, run this command to migrate your file
php artisan migrate:specific database/migrations/table.php

Related

Creating Schema automatically using migration in Laravel

Is it possible to create schemas automatically in the database when xampp is turning on? There is no information about it in the Laravel documentation, documentation says only how to do it manually by writing the command php artisan migrate.
Xampp includes a script called apache_startup.bat in the root directory, you can add the following line to it
c:\xampp\php\php.exe -f /path/to/laravel/app/artisan migrate
Then when the apache server starts, it should run that the equivalent of php artisan migrate to setup the database.
You can do php artisan make:migration [migration-name] - other than that, you have to fill the rest yourself.

Laravel create migration file in specific folder

I've created Payment package for my laravel project
I want to make migration files inside migrations folder of my package. How can create it using artisan command?
I want something like
php artisan make:migration packages/Payment/src/Database/add_orderId_to_cart_payment_table
Use this command on the root folder:
create the only Migration file:
php artisan make:migration create_products_table --create=products
Create Migration, Model file:
php artisan make:model Product -m
For Create Migration,Model,Controller file:
php artisan make:model Product -mcr
If you want to do it manually then you may set --path as per your folder requirement.
php artisan make:migration filename --path=/app/database/migrations/relations
php artisan make:migration filename --path=/app/database/migrations/translations
If you want to migrate then:
php artisan migrate --path="/app/database/migrations/relations"
For specific directories:
php artisan make:migration create_users_table --path=/packages/Payment/src/Database
The new migration will be placed in your packages/Payment/src/Database directory.
For Running Migrations: php artisan migrate --path=/packages/Payment/src/Database
Generating a migration using the inbuilt framework feature:
php artisan make:migration create_users_table
The new migration will be placed in your database/migrations directory.
For Running Migrations: php artisan migrate
you need to publish them from your package to your migrations folder like this in your package service provider boot method:
$this->publishes([
__DIR__.'/Database/migrations/' => database_path('migrations'),
], 'migrations');
run this command php artisan vendor:publish --tag=migrations
and after that you can run php artisan migrate
You can run:
php artisan make:migration --help and see all the options available for you to use.
It seems you are making a package. It has nothing to do with where to create migrations, just create migrations in regular way, run your migrations and simply put them in your package/src/Database/migrations/yourMigrations_xxxxxx.php and in your package serivce provider in boot() method write this line
$this->loadMigrationsFrom(__DIR__ . '/Database/migrations');
laravel has a built in method loadMigrationsFrom('path/to/migrations') to pick up migrations, when someone installs this package and when he'll run php artisan migrate it'll run all the migrations whether they are in the user's Database/migrations or in your package's package/src/Database/migrations/2022_03_29_23424_create_countries_table_.php, all the migrations will run

I can not migrate tables again

I deleted all tables in database, but my error is :
table.questions exists
so I can not migrate them, does it depend on something else as well?
You can run:
php artisan migrate:refresh
to rollback all the tables (including the migrations table) and recreate everything again.
It's impossible if you really removed all the tables from database.
Make sure you really deleted all tables including migrations table.
Normally when you want to revert all migrations you should run:
php artisan migrate:reset
If you haven't done this assuming you have are running Laravel 5.5 you can run:
php artisan migrate:fresh
I would advise you to read the whole Migrations documentation to know how they work and how you should create them. In fact you should not remove tables manually and just run:
php artisan migrate:rollback
to rollback migrations previously run or as I already said you can run one one previously mentioned commands to rollback or rollback and run again your migrations.
You get this error because you already have this table in DB. You need to drop a table in down() method of each migration and use php artisan migrate:rollback command to delete last batch of executed migrations.
In 5.5 you also can use php artisan migrate:fresh if you do not drop tables in the down() method.
https://laravel.com/docs/5.5/migrations#rolling-back-migrations
Or you can just manually recreate a DB and run php artisan migrate command.
I found it , I had a middleware in boot for question table and added this:
if(\Schema::hasTable('questions')) {
....
}

Laravel Migrate Specific File(s) from Migrations

Hi read all the included documentation here in https://laravel.com/docs/5.4/migrations.
Is there a way on how to migrate a certain migration file (1 migration only), cause right now every time there is a change I use php artisan migrate:refresh and all fields are getting reset.
First you should create one migration file for your table like:
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 create test folder in migrations folder then newly created migration moved/copied in test folder and run below command in your terminal/cmd like:
php artisan migrate --path=/database/migrations/test/
you should add the path to your migration file to refresh just this table and run
php artisan migrate:refresh --path=/database/migrations/fileName.php
Just look at the migrations table in your database, there will be a list of migration file name and batch number value.
Suppose you have 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 want to just rollback 2016_09_07_103432_create_tabel_roles migration,
change it's migration batch value to 2 which is highest among all and then just execute following.
php artisan migrate:rollback
Here only table with batch value 2 will be rolled back. Now, make changes to that table and run following console command.
php artisan migrate
Batch value in the migrations table defines order of the migrations. when you rollback, migrations that are latest or have highest batch value are rolled back at first and then others. So, you can change the value in database and then rollback a particular migration file.
Although it's not a good idea to change batch number every time because of relationship among the table structure, we can use this case for some cases where single table rollback doesn't violates the integrity among the tables.
Hope you understand.
if you use tab for autocomplete
php artisan migrate --path='./database/migrations/2019_12_31_115457_create_coworking_personal_memberships_table.php'
php artisan migrate --path=database/migrations/2020_04_10_130703_create_test_table.php
Note :
after --path no / before
You can run command like this
php artisan migrate --path=/database/migrations/2020_04_10_130703_create_test_table.php
You need to put the file(s) into a new directory (ex:selected) and then apply
php artisan migrate --path=/database/migrations/selected
if you need rollback:
php artisan migrate:rollback --path=/database/migrations/selected
Note:
php artisan migrate:refresh
this will rollback and then migrate all the migrations files in the default directory (/database/migrations)
For Specific File run this command:
php artisan migrate:refresh --path="database/migrations/Your_Migration_File_Name_table.php"
Here --file= is for location of your file and migrate:refresh will empty your table data
If you want to empty all table's data from database then run
php artisan migrate:refresh command.
php artisan help migrate
You will see the option:
--path[=PATH] The path to the migrations files to be executed
By the way, you can probably indicate the root folder of the file that you want to migrate:
php artisan migrate --path=/database/migrations/sample.php
Or, You can create a new folder in migrations, then migrate all the migration files you want inside it:
php artisan migrate --path=/database/migrations/new_folder
You can only run this command in your terminal
php artisan migrate --path=database/migrations/2020_10_01_164611_create_asset_info_table.php
After migrations you should put the particular file name. or if you have any folder inside migration then just add that folder name after the migration.
Like this
php artisan migrate --path=database/migrations/yourfolder/2020_10_01_164611_create_asset_info_table.php
I hope this will help you a lil bit. Happy Coding.
Just use the --path flag. You don't need to use rollback, refresh, or any other command.
Example:
php artisan migrate --path=/database/migrations/migration_file_name.php
Get the actual file name and path of the migration and run the command like below
php artisan migrate --path=/database/migrations/2021_10_03_071450_create_reset_codes_table.php
If you want to create one and specific table. You can use this code. It works for laravel(5.x) versions.
php artisan migrate:refresh --path=/database/migrations/fileName.php
Just wanted to post another solution, which i think is worth mentioning.
Find row with your migration name in migrations table and DELETE it.
It should look like this: 2016_06_01_000001_create_oauth_auth_codes_table
Remove your table from database e.g. DROP TABLE oauth_auth_codes
Run php artisan migrate
It will migrate only the table you need, and won't touch anything else
Correction- remove slash before the database
$ php artisan migrate --path=database/migrations/migration.php
php artisan migrate --path=/database/migrations/fileName.php
You don't have to refresh for migration, because refresh means : Rollback all migrations and run them all again.
You can only rollback:
php artisan migrate:rollback
https://laravel.com/docs/5.4/migrations#rolling-back-migrations
You can specify how many migrations to roll back to using the 'step' option:
php artisan migrate:rollback --step=1
Some tricks are available here:
Rollback one specific migration in Laravel
Delete the table and remove its record from migration table.
After that you just run migration again:
php artisan migrate
Specific Table Migration
php artisan migrate --path=/database/migrations/fileName.php
Or you can simply delete migration file name from your database, in "migrations" table and then run : php artitsan migration
If you want to create another table, just create a new migration file. It'll will work.
If you create a migration 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 field like "status" without migrate:refresh. 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 the migration with php artisan migration, just migrate the new migration file.
But if you add field "status" into the first migration file (users_table) and run migration. It's nothing to migrate. You need to run php artisan migrate:refresh.
Hope this help.
Use: --path=database/migrations/your_migration_file_name.php
Examples:
php artisan migrate:refresh --path=database/migrations/your_migration_file_name.php
php artisan migrate:rollback --path=database/migrations/your_migration_file_name.php
php artisan migrate --path=database/migrations/your_migration_file_name.php
References: Genarate laravel migration
You could try to use the --path= option to define the specific sub-folder you're wanting to execute and place specific migrations in there.
Alternatively you would need to remove reference and tables from the DB and migrations tables which isn't ideal :/
install this package
https://github.com/nilpahar/custom-migration/
and run this command.
php artisan migrate:custom -f migration_name
php artisan migrate --path=/database/migrations/fileName.php
Just follow the instruction execute this commant file name here should be your migration table name
Example:
php artisan migrate --path=/database/migrations/2020_02_21_101937_create_jobs_table.php
You can use:
php artisan migrate:refresh --path=/database/migrations/<migration table here>
You can use this.
-> https://packagist.org/packages/sayeed/custom-migrate
-> https://github.com/nilpahar/custom-migration/
this is very easy to use
you can also migrate again migrated table but first you need to go in database migration table and delete row that specific migration name.
and then hit php artisan migrate
First you should to make the following commands:
Step 1:
php artisan migrate:rollback
Step 2:
php artisan migrate
Your table will be back in database .

How to automatically run the migrations in a Laravel package?

I just installed Cartalyst's Sentry 2 in a Laravel 4 application but I found out that I have to run that package's migrations separately by specifying --package=cartalyst/sentry, which makes automatic deployment impossible.
Is there a way to run php artisan migrate and have it run Sentry's migrations as well?
After Laravel 5 there is a better way to solve this:
Create your migrations in package's
/database/migrations folder
.
After that, in package's service provider create a boot method, referencing the migrations folder
public function boot()
{
$this->loadMigrationsFrom(__DIR__ .
'/database/migrations');
}
Now, on the top level folder (the main app, which required this package), you can run the default migrate command ( php artisan migrate ) and it will automatically find the packages migrations througth the loadMigrationsFrom method
ANSWER COPIED FROM:
http://voerro.com/en/tutorials/r/developing-and-distributing-laravel-5-packages/3
What I usually do in a scenario like this is publish the package migrations though the command:
php artisan migrate:publish vendor/package
This copies the migration files from any given package to your migrations folder.
I created a composer script to replace php artisan migrate ...... The script runs my migrations and the vendor's migrations everything at once.
My composer scripts is
"scripts": {
"migrate": [
"php artisan migrate --env=$LARAVEL_ENV",
"php artisan migrate --package=\"cartalyst/sentry\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"mrjuliuss/syntara\" --env=$LARAVEL_ENV",
"php artisan migrate --package=\"filmoteca/static-pages\" --env=$LARAVEL_ENV"
]
}
Then you can run the migration with LARAVEL_ENV=prod composer run-script migrate
To pass parameter to the script I use environment variables. In the previous example I set the environment variable LARAVEL_ENV to prod so the migration use the production database connection.
You can always create a alias in your local machine to short the command. For example alias migrate="LARAVEL_ENV=local composer run-script migrate"
I think this approach is good because when you are going to add a new package to your composer.json and this package has a migration you add the package and the package's migration in the same file. So, you do not forget add/remove the migration of a package.
This is a complete composer.json with the script
The 3rd party package must declare a Service Provider, that declare migrations.
As you can see at https://github.com/laravel/passport/blob/9.x/src/PassportServiceProvider.php#L80:
protected function registerMigrations()
{
if (Passport::$runsMigrations && ! config('passport.client_uuids')) {
return $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}
}

Resources