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

leostereo's avatar

Where should I write business logic on laravel application ? (or any other MVC architecture)

Hi guys , this time im bringing this discussion to table since I can not explain this job interview question.

Short answer is , you should write your business login in the model.

Why ? I understood that model is related to database objetcs models.

Of course I provide the "in the controller" wrong answer.

Could you provide some light / feedback / opinion here ?

I spent most of the time writting my code on controller clases , so It is not clear for me where/how to write code on the model.

Thanks!

0 likes
14 replies
Tray2's avatar

There is a saying with MVC, that goes something like this.

Keep your controllers skinny, your models fat, and your views dumb.

I hope I remembered that one correctly.

Where to put the logic, well it depends on what company you work for. There are many development patterns that you can use, some say that you should put the logic into a repository class that you then use in your controller (Reposotiry pattern).

Then again it depends on what you mean by business logic, if you are refering to database queries, I'd say to keep them in your controller or in your model, but then again it's always your employer who decides which pattern to use.

I think you should read up on some of the patterns and see what they all have to offer, then you can ask them which pattern they are using and give them the answer they want.

3 likes
leostereo's avatar

Excellent , can you please explain , why controller should be skinny ?

I heard that , controllers should be like a bridge between models and view , but im not sure why ...

thanks

1 like
Sinnbeck's avatar

@leostereo let me give you a simple example.

You write some great code in a controller for getting a specific number of rows and transform it into a format you can use.. Now later you find out you actually need the same thing in a console command (artisan). So do you want to use that controller? It expects a request.. Console does not have requests.. And it returns a view.. But you don't want to use a view.. So you need to write exactly the same code again in the console command

1 like
leostereo's avatar

@Sinnbeck thanks .... thanks .... Im thining that perhaps , taking code to helper / action claseses , same code can be used also by other controllers as well.

martinbean's avatar

Short answer is , you should write your business login in the model.

Why ? I understood that model is related to database objetcs models.

Of course I provide the "in the controller" wrong answer.

Could you provide some light / feedback / opinion here ?

@leostereo Models are representation of business entities, and also business processes (logic). You use models to model the real world. So a model ends up being what your app is and does.

If you write your business logic in controllers, then you can’t re-use that business logic in other contexts, such as console commands or queue jobs. You shouldn’t have to copy and paste “business logic” in various parts of your application.

A controller should just take input, invoke business logic, and render the results of that operation in a view, as JSON, etc. Same with a console command: it should take user input from command line arguments/options, invoke business logic, and print the results to the terminal.

2 likes
timmer's avatar

Speaking for myself, I tend to think of the controller code as the entry point to the logic, sort of the "main" function, which calls other things in a top/down way. If it needs to hit the database, I might add a method to the model. If it's making requests to a third party API, let's say Jira, I'll wrap all that stuff in a custom Jira service class. Miscellaneous other calls may go in a helper class or just a file with helper functions; could be a generic helper, or one dedicated to this particular topic/activity.

Typically this leaves the controller code as very short and readable, mostly making calls to methods/functions defined elsewhere.

jlrdw's avatar

@leostereo Well @jeffreyway has mentioned countless times, there are many ways to do things, and sometimes there is no "right way"

I know when I was programming in java, yes I normally had MVC. However for some things small I would sometimes use Servlet and view (CV).

Same in PHP, let it depend on what you are doing. I know many here put a bunch in the controller.

I normally don't, I do like @tray2 mentioned, smaller controller and fat models.

Doing a query to retrieve = model

A query to update, many times controller is fine.

And don't get stuck on some of those "patterns" I find that sticking to MVC is all that's needed. Yes implement DRY.

jlrdw's avatar

@leostereo

For example .... switch from laravel to YII reutilizing most of the model.

You would still have a lot of code rewrite, because Yii 2 ORM is still different from Laravel.

Whereas Using laravel getPdo() and Cake\Datasource\ConnectionManager it would be the exact same code, just a few tweaks are needed.

I have converted cake to laravel. But I use regular sql 75 percent of the time.

Edit:

Same with Yii 2, Yii 2 also of course has an instance, I have used like:

    public static function dbh()
    {

        try {
            $dbh = \Yii::$app->db;
            $dbh->open();
            return $dbh;
        } catch (PDOException $e) {
            throw new pdoDbException($e);
        }
    }
1 like
Martynnycob's avatar

Controllers are responsible for handling requests, interacting with Models, and managing the flow of your application. They act as intermediaries, but the heavy lifting should be done in the Model. Think of it as the Model describing "how" your data behaves.

So, if you're wondering where to write code in the Model, you can create methods within your Model classes to handle specific business logic related to that data. For instance, if you're working on a User model, you might create methods for user-related operations, like updating a user's profile, verifying credentials, etc.

If you're spending most of your time in controllers, it might be worth reevaluating how you're structuring your application. It's all about achieving that balance. Also, you might find great resources and insights on this topic at bluetallyapp.com. They have some fantastic content on Laravel and MVC best practices.

Please or to participate in this conversation.