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

Paeolen's avatar

Design Pattern: how do you structure your Eloquent models?

Hello,

My models can become quite big and less readable as they hold many things, including static repo-like functions (as Laravel don't use repositories, I don't really either even if I probably could), relations, scopes, custom functions, accessors and mutators.

I was wondering if splitting the code (How? Traits?) would help or it would become difficult to know where the data comes from...

I don't really know if there are other good options/patterns than the all-in-one model that we often see in Laravel. Have you ever structured your models differently?

0 likes
1 reply
D9705996's avatar

There isn't one correct answer to this question. It really depends on your application, coding standrds you/your team have adopted, etc.

However there is a great series here on Whip Monstrous Code into Shape that covers dealing with complex code.

I am also starting to look at splitting my rather large monolithic application into individual packages to reduce complexity. The documentation seems straight-forward and I think this is a good path but only time will tell! Automatic Service Provider registration looks like it could help make a modular system

Please or to participate in this conversation.