,

Create Migration and Model by Artisan Command in Laravel

Posted by

The Laravel create migration and model command are the main topics of this lesson. You’ll learn how to use CMD to create migrations and models for Laravel. We will assist you by providing a model and migration example created by a laravel artisan. It is a straightforward illustration of a migration and model made with PHP. Let’s begin by running the create migration command in the Laravel model.

This is just a quick suggestion, but you can use the artisan command to build migration and models simultaneously. We will create a model with migration using the php artisan make command. Let’s look at the commands now.

php artisan make:model mytable --migration
php artisan make:model mytable -m

You will see the following model and migration.

<?php
  
namespace App\Models;
  
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
  
class mytable extends Model
{
    use HasFactory;
}

<?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('mytables', function (Blueprint $table) {
            $table->id();
            $table->timestamps();
        });
    }
  
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('mytables');
    }
};
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x