Folders-Collage

Laravel Models folder: To Be or Not To Be

This short post is inspired by some discussions that appear constantly in various forums or social media channels. Question of the day/month/year: why is there no app/Models folder? And do we need one?

Once upon a time there was a folder

Does anyone remember Laravel 4? There was a clear MVC folder structure there:
app:

  • controllers
  • models
  • views

It was really easy to understand what to put where. At that time, to be honest, a lot of custom logic was put directly into models, therefore their importance was quite big.


Revolution in Laravel 5.0

People started panicking when default Laravel 5.0 installation didn’t contain app/models folder anymore, along with other folder structure changes like app/Http or resources/views.

From then default models were created directly in app/ folder, like app/User.php.

At first there was a rant from the community – why the change?

laravel y u no models

The main reason is to support bigger application architecture with Laravel – separating the concerns and introducing layers like app/Http (everything related to routes) and resources (not only views but front-end stuff to compile).

In this case models were kind of in the middle of everything. So they remained in just app/ folder. And it looks ok but not if project contains 20+ models, it becomes cluttered.


Guess what, you can have your folder

Of course, people started defending Taylor and offering a workaround – you can actually still have app/Models folder and put all the models there, as soon as you have a proper namespace inside:

Also you can even generate models with artisan commands this way:

The result is the file app/Models/Book.php:


Or maybe use DDD?

There’s another theory on how to structure your application folders – by domain, or “topic”. So have subfolders for every topic and then inside of it have Models, Providers and whatever else you want.

laravel ddd folders

It won’t be a default Laravel folder structure, but some developers are big fans of DDD (Domain-Driven Design).

Read more here:

Want to generate Laravel adminpanel online?
You don't need any packages to do that!

Leave a Reply

Your email address will not be published. Required fields are marked *