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

mennor's avatar

How to keep your Models small?

Hi all,

Yesterday I saw a great video by Adam Wathan about how to keep your Controllers small (https://www.youtube.com/watch?v=MF0jFKvS4SI). It made a lot of sense and it will be really helpful for future projects.

However, I could not find good explanations on how to keep your Models small. I have a project where both the User.php and another important model have hundreds of lines of code: relations, setters, getters, lots of scopes, ..... When opening the file you can only see the long list of 'use ....' statements that fill the screen.

Any tips on this?

Thanks!

0 likes
2 replies
crnkovic's avatar

Okay. This is where I shine. My models are usually very clean. If models have couple of relationships, I keep them in a model, usually if I have 10+ relationships, I'll extract them into App\Relationships\UserRelationships trait or something similar. Custom queries go into their specific file (App\Repositories\Users, or App\Queries\Users), observers go into their own classes. Same goes with custom factories (when I need more than Model::create), or any heavy logic (user avatar goes into Services\Avatar class, and $user->avatar() just returns a new instance of the Avatar service class).

Regarding imports, if you have like a service class called Markdown located in App\Services and you're using it only in one place in your model (class) within App namespace, there is no need to import App\Services\Markdown, I usually just reference relative path to the class: Services\Markdown, one line less in use imports :)

So all in all, models usually contain logic that operate on a single instance, such as $post->publish, $user->confirm, scopes and relationships.

1 like

Please or to participate in this conversation.