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

thebigk's avatar
Level 13

How to handle large number of models?

My app requires me to create large number of Models ( ~ about 30, minimum ) and I'm wondering if I should keep adding the files under the 'app' directory, which seems to be the default location for model files.

Is there a way to organise these models into separate folders of their own so that they are easier to manage and access? What are the immediate changes I'll be required to do if I put the models inside folders like:

/app/event/event.php /app/event/organizer.php

/app/job/job.php

app/blog/post.php app/blog/comment.php

and so on?

0 likes
9 replies
jlrdw's avatar

You can put models under another user defined folder, just namespace correctly.

Taylor says

Models typically live in the app directory, but you are free to place them anywhere that can be auto-loaded according to your composer.json file. 

Also, some of the database tables where there are no relations just use the querybuilder. Just place

use Illuminate\Support\Facades\DB;

in controller.

1 like
ricardovigatti's avatar

Do not use DB on your controller, since your intention is to have a large number of files, this will cause your controllers to grow faster. My advice is to create a folder Models and fill it all with models, exactly as @jlrdw 's first suggestion.

/app
    /Models
        // other models
        FooModel.php
        BarModel.php

Remember to change namespaces.

1 like
thebigk's avatar
Level 13

Thank you for your suggestions @jlrdw and @ricardovigatti . So I guess all models should be inside ONE parent folder; and not multiple folders.

In that case, I think it'd better to have them in their default position.

Snapey's avatar

nothing to stop you having app/Models/Job/Job.php and having a folder per category of model

It just depends if it makes it easier for you to group them or just have an alphabetical list

thebigk's avatar
Level 13

@Snapey - But will I lose the ability to create models with Artisan? What files or specific changes will be required to make stuff work with Laravel's tools?

Cronix's avatar
Cronix
Best Answer
Level 67

you can use artisan...just include the namespace relative to "app" (on my mac anyway).

artisan make:model Test/Test

creates /app/Test/Test.php model

And it namespaces it automatically as App\Test.

You can also create it like artisan make:model Test\\Test (escaping the slash with a slash) and it will create the same as above. I prefer the first method because in my mind I'm creating a directory and file, and the other way with 2 backslashes is more like creating a namespace, which seems weird from the cli.

1 like
jlrdw's avatar

So I guess all models should be inside ONE parent folder; and not multiple folders.

No that is not what I said, I showed what Taylor has in the docs, you can have 50 different model folders if you wish, just namespace them correctly.

fabricecw's avatar

Some of our applications also have a large number of models, but with a strict naming concept it's no problem to handle it. But sure, it depends on your needs.

1 like
ricardovigatti's avatar

So I guess all models should be inside ONE parent folder; and not multiple folders.

You can use multiple level of folders if necessary.

/app
    /Models
        Auth
            UserModel.php
            UserRelatedModel.php
        FooModel.php
        BarModel.php

Please or to participate in this conversation.