Laravel : Create Model with Migration, Controller and generate DB Table

Laravel : Create Model with Migration, Controller and generate DB Table

Creating a Model in Laravel

Laravel makes it very easy to create a new Model.
A Model represents the M part of the MVC (Model-View-Controller) architecture.
It is a PHP class that maps to a database table and performs Eloquent ORM operations.

Step 1: Create a New Model

Let's say you are building a Blog Application. Every blog application has posts, so you'll first create a Post model.

Open your Terminal or Command Prompt, navigate to your Laravel project root, and run:

php artisan make:model Post -mr
  • -m generates a migration file.

  • -r generates a resourceful controller.

After running the command, you'll notice:

  1. A new Post.php model is created under the App\Models directory (in older Laravel versions, under App directly):

<?php namespace App\Models; // or App in older versions use Illuminate\Database\Eloquent\Model; class Post extends Model { // }
  1. A new migration file is created under database/migrations/ (something like 2025_04_29_000000_create_posts_table.php):

<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); }); } /** * Reverse the migrations. */ public function down() { Schema::dropIfExists('posts'); } }
  1. A new resourceful controller is created under App\Http\Controllers:

<?php namespace App\Http\Controllers; use App\Models\Post; // Adjust namespace if needed use Illuminate\Http\Request; class PostController extends Controller { /** * Display a listing of the resource. */ public function index() { // } // Other methods: create, store, show, edit, update, destroy }

Step 2: Define Table Structure

Now, let's update the migration file to include fields that posts typically need, such as title and body.

Edit the up() Method in your migration like this:

public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('body'); $table->timestamps(); }); }
  • title: A short text for the post's title (using string).

  • body: A longer text for the post's content (using text).

Step 3: Run the Migration

Now, create the database table by running:

php artisan migrate

This command will generate the posts table in your database.

Summary

  • You created a Model, a Migration, and a Resourceful Controller.

  • You updated the migration to define the posts table fields.

  • You ran the migration to create the actual database table.

Now you're ready to start working with posts in your Laravel application!

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close