How to Create Migration in Laravel 10?
Hey Dev,
In this article, we will cover how to create database table using migration command in laravel 10. you can see how to create migration in laravel 10. Here you will learn how to create table migration in laravel 10. In this article, we will implement a laravel 10 create table using migration. follow the below step for create table in laravel 10 using migration.
I will guide you on how to create a database table using laravel migration. we will use laravel 10 commands to create migration for the table.
In this example, we will simply create a "posts" table with id, title, body, is_publish, created_at, and updated_at columns. we will generate new migration by laravel 10 command and add the above rows. Then we will run the migration and we will get a table created on MySQL database. so let's see the below tutorial.
Create Migration:
Using bellow command you can simply create migration for database table.
php artisan make:migration create_posts_table
After run above command, you can see created new file as bellow and you have to add new column for string, integer, timestamp and text data type as like bellow:
database/migrations/2022_02_17_133331_create_posts_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->boolean('is_publish')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('posts');
}
};
Run Migration:
Using bellow command we can run our migration and create database table.
php artisan migrate
After that you can see created new table in your database as like bellow:
Create Migration with Table:
php artisan make:migration create_posts_table --table=posts
Run Specific Migration:
php artisan migrate --path=/database/migrations/2023_04_01_064006_create_posts_table.php
Migration Rollback:
php artisan migrate:rollback
I hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel 10 Mail | Laravel 10 Send Mail Tutorial
- Laravel 10 Eloquent Mutators and Accessors Example
- Laravel 10 Create Custom Helper Functions Example
- Laravel 10 Authentication using Jetstream Tutorial
- Laravel 10 Import Export Excel and CSV File Tutorial
- Laravel 10 Authentication using Breeze Tutorial
- How to Send Email using Gmail in Laravel 10?
- Laravel 10 Bootstrap Auth Scaffolding Tutorial
- Laravel 10 Generate PDF File using DomPDF Example
- Laravel 10 Multiple File Upload Tutorial Example
- Laravel 10 Multiple Image Upload Tutorial Example
- Laravel 10 File Upload Example Tutorial
- Laravel 10 Form Validation Tutorial Example
- Laravel 10 Image Upload Example Tutorial