PHP › MVC Architecture in PHP

How do Models in MVC represent both data structure and business logic, and how does this differ from a Model being purely a simple database representation?

Intermediate

Answer

While a Model often does directly correspond to a database table, particularly when using an Active Record based ORM like Eloquent, a well designed Model in MVC should also encapsulate relevant business logic and rules specific to that data, such as validation rules, computed properties, or domain specific behaviors, rather than being purely a passive container for database columns, ensuring that business rules live consistently in one place rather than being scattered throughout multiple controllers that happen to work with the same data.
class Order extends Model {
    public function isEligibleForRefund(): bool {
        return $this->status === 'delivered' && $this->created_at->diffInDays(now()) <= 30;
    }
}
Real-world example An order refund feature checks a business rule about refund eligibility by calling a dedicated method directly on the Order model itself, ensuring that exact same rule is applied consistently wherever it is needed throughout the application rather than being duplicated across multiple controllers.

Common follow-ups: How do you decide whether specific business logic belongs on the model or in a separate dedicated service class?;What is the difference between a 'fat model' and a 'thin model' approach?