When you create migrations in laravel, you also have command to migrate it and create tables schemas in database. Migration file is the file which creates the table structure.
When we use migrate command from artisan command list, it will run all migrations and create table. But in some cases we want to run only a specific migration instead to run all.
Inside this article we will see the concept of migrate specific migration file in laravel 8. This command is not laravel 8 specific, you can use it in with any version.
Let’s get started.
Laravel Installation
We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.
Here is the command to create a laravel project-
composer create-project --prefer-dist laravel/laravel blog
To start the development server of Laravel –
php artisan serve
URL: http://127.0.0.1:8000
Assuming laravel already installed inside your system.
Create Database & Connect
To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.
CREATE DATABASE laravel_app;
To connect database with application, Open .env file from application root. Search for DB_ and update your details.
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=laravel_app DB_USERNAME=root DB_PASSWORD=root
Create Migration
By default after fresh installation of laravel, when you will see into /database/migrations folder you should see 3 default migration files.
To create new migration file, here is the artisan command.
Open project into terminal and run this command.
$ php artisan make:migration create_products_table
It will create a migration file with name like 2021_05_01_092040_create_products_table.php inside /database/migrations folder.
Open migration file and write this complete code.
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateProductsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('products', function (Blueprint $table) { $table->id(); $table->string("title", 120); $table->integer("amount"); $table->string("prouduct_thumbnail", 170)->nullable(); $table->text("description"); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('products'); } }
Migrate Migrations in Laravel
We have artisan command to migrate laravel migration files.
Migrate All Migrations
$ php artisan migrate
It will migrate all migrations from /database/migrations folder create their respective tables.
Migrate a Specific Migration File
$ php artisan migrate:refresh --path=database/migrations/2021_05_01_092040_create_products_table.php
Syntax – $ php artisan migrate:refresh –path=<MIGRATION FILE PATH>
Inside this case, migration file path is database/migrations/2021_05_01_092040_create_products_table.php
We hope this article helped you to learn about Migrate Specific Migration File in Laravel 8 Tutorial Example in a very detailed way.
If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.