LARAVEL

laravel Artisan migration command


To run all migrations in Laravel using Artisan, you can use the following command:

 

make:migration:

  • This command is used to create a new migration file. Migration files are used to define changes to your database schema.
  • Example usage: php artisan make:migration create_users_table

 

migrate:

  • The migrate command runs all pending migrations that have not yet been executed.
  • Example usage: php artisan migrate

 

migrate:fresh:

  • The migrate:fresh command rolls back all migrations and then re-runs them. It essentially resets your entire database schema.
  • Example usage: php artisan migrate:fresh
  •  

migrate:install:

  • The migrate:install command creates the migration repository, which is used to store information about which migrations have already been run.
  • Example usage: php artisan migrate:install

 

migrate:refresh:

  • This command rolls back all migrations and then re-runs them. It is similar to migrate:fresh, but it also re-seeds the database after running migrations.
  • Example usage: php artisan migrate:refresh

 

migrate:reset:

  • The migrate:reset command rolls back all migrations. It essentially undoes all migrations that have been applied.
  • Example usage: php artisan migrate:reset

 

migrate:rollback:

  • This command rolls back the last batch of migrations that were run.
  • Example usage: php artisan migrate:rollback

 

migrate:status:

  • The migrate:status command shows the status of each migration: whether it has been run or not.
  • Example usage: php artisan migrate:status

 

Some Use Cases

# Step 1: Create a migration to create a "users" table
php artisan make:migration create_users_table

# Step 2: Define the schema for the "users" table in the generated migration file
 	use Illuminate\Database\Migrations\Migration;
	use Illuminate\Database\Schema\Blueprint;
	use Illuminate\Support\Facades\Schema;

	class CreateUsersTable extends Migration
	{
    	public function up()
    	{
        	Schema::create('users', function (Blueprint $table) {
            	$table->id();
            	$table->string('name');
            	$table->string('email')->unique();
            	$table->timestamp('email_verified_at')->nullable();
            	$table->string('password');
            	$table->rememberToken();
            	$table->timestamps();
        	});
    	}

    	public function down()
    	{
        	Schema::dropIfExists('users');
    	}
	}			
 
 
# Step 3: Run the migration to create the "users" table in the database
php artisan migrate

# Step 4: Later, you might want to add a new column to the "users" table
php artisan make:migration add_role_to_users_table

# Step 5: Define the schema for the new column in the generated migration file

# Step 6: Run the new migration to add the new column to the "users" table
php artisan migrate

# Step 7: Suppose you want to rollback the last migration batch
php artisan migrate:rollback

# Step 8: You might want to refresh the entire database schema
php artisan migrate:fresh

# Step 9: Finally, you can check the status of migrations
php artisan migrate:status

# Step 10 If you want to run only a specific migration file, you can use the --path option
php artisan migrate --path=/database/migrations/filename.php

Note - These commands are essential for managing your database schema and keeping it in sync with your application's codebase when using Laravel. They allow for efficient version control of your database structure and facilitate smooth development and deployment processes.


LARAVEL