create table in laravel using Migration and Manage Database

create table in laravel – Run Migration: Using bellow command we can run our migration and create database table. I want to create the in the table how to do this.

create table in laravel in Database

Artisan, creating tables in database : there are following the steps of the “Create database tables in Laravel”.

Step : 1

First of all, Generate a migration script for creating a authors table using artisan on command line. It will make a php file in database/migrations/2022_04_25_1012154_create_articles_table.php

php artisan make:migration create_articles_table --create=articles

Step : 2

Open the file database/migrations/2022_04_25_1012154_create_articles_table.php, it should look like this

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('articles');
    }
}

Step : 3

Update the file database/migrations/2022_04_25_1012154_create_articles_table.php to add some more fields and foreign key.

Read Also:  How to update query in PHP MySQL?

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateArticlesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('author_id')->index()->unsigned();
            $table->date('date')->index();
            $table->string('title_name');
            $table->string('description');
            $table->timestamps();
            $table->foreign('author_id')
                  ->references('id')->on('authors')
                  ->onDelete('cascade');  
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('articles');
    }
}

Step : 4

create table in laravel – Simple run this cmd to Generate a Article model using artisan on command line. It will make a model class in app/Article.php

Read Also:  PHP String Concatenation Example with demo

php artisan make:model Article

Step : 5

Now, Open up the file app/Article.php, you should see something similar to this.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Article extends Model
{
    //
}

Step : 6

For adding more data types when creating a database table in Laravle’s migration script on phase 3, take a look at this file vendor/laravel/framework/src/Illuminate/Database/Schema/Blueprint.php

Don’t Miss : Laravel : Create Model with Migration, Controller and generate DB Table


I hope you get an idea about create table in laravel.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.