To run all migrations in Laravel using Artisan, you can use the following command:
make:migration
:php artisan make:migration create_users_table
migrate
:migrate
command runs all pending migrations that have not yet been executed.php artisan migrate
migrate:fresh
:migrate:fresh
command rolls back all migrations and then re-runs them. It essentially resets your entire database schema.php artisan migrate:fresh
migrate:install
:migrate:install
command creates the migration repository, which is used to store information about which migrations have already been run.php artisan migrate:install
migrate:refresh
:migrate:fresh
, but it also re-seeds the database after running migrations.php artisan migrate:refresh
migrate:reset
:migrate:reset
command rolls back all migrations. It essentially undoes all migrations that have been applied.php artisan migrate:reset
migrate:rollback
:php artisan migrate:rollback
migrate:status
:migrate:status
command shows the status of each migration: whether it has been run or not.php artisan migrate:status
# 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.