Creating Models and Tables Using Migration in Laravel 10

Yash Kumar Prasad
2 min readOct 27, 2024

Introduction

Laravel 10 introduces an elegant way to handle database migrations and models, making it easy for developers to build scalable applications. In this blog post, we’ll walk through the process of creating a model and a corresponding database table using Laravel migrations.

What are Migrations?

Migrations are like version control for your database, allowing you to define the structure of your tables in PHP code. This makes it easy to manage database schema changes over time.

Step 1: Setting Up Your Laravel Project

First, ensure you have Laravel 10 installed. You can create a new Laravel project using Composer:

composer create-project laravel/laravel your-project-name

Navigate into your project directory:

cd your-project-name

Step 2: Creating a Migration

To create a new migration, use the Artisan command:

php artisan make:migration create_posts_table

This command generates a new migration file in the `database/migrations` directory. The file name includes a timestamp for version control.

Step 3: Defining the Migration

Open the newly created migration file (e.g., `2023_10_27_000000_create_posts_table.php`). You’ll find two methods: `up()` and `down()`. The `up()` method is used to define the schema, while the `down()` method should reverse the actions of `up()`.

Here’s how to define the `posts` table:

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

class CreatePostsTable extends Migration
{
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('posts');
}
}

In this example, we’re creating a `posts` table with three columns: `id`, `title`, and `content`, along with timestamps for `created_at` and `updated_at`.

Step 4: Running the Migration

To create the table in your database, run the following command:

php artisan migrate

This command executes all pending migrations. After running it, you should see the `posts` table created in your database.

Step 5: Creating a Model

Now that we have a database table, we can create a model to interact with it. Use the following command:

php artisan make:model Post

This command creates a new model file in the `app/Models` directory.

Step 6: Defining the Model

Open the `Post.php` model file and define the properties:

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
use HasFactory;

protected $fillable = ['title', 'content'];
}

The `fillable` property allows mass assignment for the specified attributes, making it easier to create new records.

Step 7: Using the Model

You can now use the `Post` model to interact with the `posts` table. Here’s a quick example of how to create a new post:

use App\Models\Post;

$post = Post::create([
'title' => 'My First Post',
'content' => 'This is the content of my first post.',
]);

Conclusion

In this tutorial, we explored how to create a migration for a new database table and a corresponding model in Laravel 10. This process simplifies database management and keeps your application organized. With Laravel’s powerful migration system, you can efficiently handle changes to your database schema as your application evolves.

Happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Yash Kumar Prasad
Yash Kumar Prasad

Written by Yash Kumar Prasad

Full-stack developer with a passion for crafting robust web solutions. Experienced in creating scalable applications that prioritize user experience.

No responses yet

Write a response