Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

anjanesh's avatar

Syncing of fieldnames in migration and model files

I am coming from Django where, inorder to create a table we HAVE TO create the model first.

Now in my test Laravel app, I created the migration first php artisan make:migration create_categories_table, added $table->string('name'); in the migration file and then php .\artisan migrate which created the table in MySQL.

Now coming to Laravel models, I did php artisan make:model Category post migration.

What is the point of a model created now if my Category table is set ? What if I add a field in my model but not in the migration file ? I am not referring to the ORM features, but rather how would the fields be synced if the same fields are not there in both the migration and model files ?

0 likes
3 replies
LaryAI's avatar
Level 58

In Laravel, migrations and models serve different purposes, and they don't need to be "synced" in the way you might be thinking. Here's a breakdown of their roles and how they interact:

  1. Migrations: These are used to define the structure of your database tables. They are like version control for your database schema, allowing you to create, modify, and delete tables and columns.

  2. Models: These represent the data and business logic in your application. They are used to interact with the database tables, typically through Laravel's Eloquent ORM.

Key Points:

  • Field Definitions: In Laravel, you don't need to define fields in the model that correspond to the database columns. The model will automatically map to the table with the same name (in plural form) and interact with all its columns.

  • Adding Fields: If you add a new field to your database table, you should update the migration file and run the migration to modify the table structure. The model doesn't need to be updated unless you want to add specific logic or attributes related to that field.

  • Mass Assignment: If you want to use mass assignment (e.g., Category::create($data)), you need to specify which fields are fillable in your model. This is done using the $fillable property.

Here's an example of how you might define a model with fillable fields:

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    // Specify the fields that are mass assignable
    protected $fillable = ['name'];
}

Workflow:

  1. Create Migration: Define the table structure.

    php artisan make:migration create_categories_table
    
  2. Run Migration: Apply the migration to create the table.

    php artisan migrate
    
  3. Create Model: Generate the model.

    php artisan make:model Category
    
  4. Update Model: Optionally, define fillable fields or any custom logic.

    protected $fillable = ['name'];
    
  5. Modify Table: If you need to add a new column, create a new migration to alter the table.

    php artisan make:migration add_description_to_categories_table --table=categories
    
  6. Run Migration: Apply the new migration.

    php artisan migrate
    

In summary, the model and migration files don't need to be "synced" in terms of field definitions. The model is more about how you interact with the data, while migrations handle the database structure.

Snapey's avatar
Snapey
Best Answer
Level 122

your model automatically handles the fields it finds in the table. There is no need to sync anything.

1 like

Please or to participate in this conversation.