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

anjanesh's avatar

Always a model for each table ?

Is it not good practice to have a model for every table ?

I mean - do this always php artisan make:model Tag -m

instead of just php artisan make:migration create_customer_tag_table and then create a model if/when required ?

0 likes
3 replies
LaryAI's avatar
Level 58

In Laravel, it's generally a good practice to create a model for each table that represents an entity in your application. Models in Laravel are used to interact with the corresponding database tables, and they provide a convenient way to encapsulate the logic related to that entity.

Here are some reasons why you might want to create a model for each table:

  1. Encapsulation of Logic: Models allow you to encapsulate the business logic related to a specific entity. This includes methods for querying the database, defining relationships, and handling any other logic specific to that entity.

  2. Eloquent ORM: Laravel's Eloquent ORM provides a powerful, active record implementation for working with your database. By creating a model, you can take full advantage of Eloquent's features, such as relationships, scopes, and accessors/mutators.

  3. Consistency and Readability: Having a model for each table can make your codebase more consistent and easier to read. It provides a clear structure and makes it easier for other developers to understand how your application is organized.

  4. Future-proofing: Even if you don't need a model immediately, creating one from the start can save time in the future. As your application grows, you might find that you need to add logic or relationships that are best handled within a model.

However, if you have a table that is purely a pivot table for a many-to-many relationship and doesn't require any additional logic, you might not need a dedicated model. In such cases, you can use Laravel's belongsToMany relationship without creating a separate model.

In summary, while it's not strictly necessary to create a model for every table, doing so is often beneficial and aligns with best practices in Laravel development. If you anticipate needing to interact with the table beyond simple CRUD operations, it's a good idea to create a model.

Here's how you can create a model with a migration using Artisan:

php artisan make:model Tag -m

This command will create a Tag model and a corresponding migration file, setting you up to define the table structure and any necessary logic in the model.

tisuchi's avatar
tisuchi
Best Answer
Level 70

@anjanesh It depends on the use case, but generally, YES.

Creating a model for every table php artisan make:model Tag -m is a good practice when the table will have logic, relationships, or be frequently interacted with in your application.

2 likes
aleahy's avatar

I think every model should have a table as a general guide, like @tisuchi says, but not every table needs a model.

If you have a relatively simple many to many relationship which requires a pivot table, making a model for the pivot table is unnecessary. As things get more complicated you may require a pivot model, but I wouldn't make one unless I needed one.

2 likes

Please or to participate in this conversation.