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

learn@karmanya.co.in's avatar

Why "Database" directory in laravel app is not namespaced?

I have seen that laravel is scanning "database" directory for migrations and other stuff instead of using some namespaces. I strongly believe that there is good reason behind this kind of design. Can anyone here tell me,

  1. why is it like that? (By googling I got an answer as "app design", but no further than this)
  2. Are there any good things which are achieved by this design, instead of using namespaces?
  3. Are there any drawbacks, if we use namespaced database directory? (may be some performance issues or something like that..)
0 likes
2 replies
Jaytee's avatar

I like to think of like this:

The database folder isn't required for your application to run. Some applications don't require a database, (i.e: a static website).

So since it isn't required, it doesn't have a reason to be namespaced. We use namespaces today so that things like composer can autoload files. We give the file the same name as the class, and composer does the rest.

The database folder doesn't have this, it has a different file name to the class name. But if you look in your composer.json file, you'll see the database folder is autoloaded using classmap. Classmap just looks for files ending in .phpor .inc.

Migrations are really a one time thing, you create the migration, migrate it and that's it. There's no need for your application to drop the tables on every request and then re-migrate.

I'm not sure if namespacing is slower, I personally would say it is since it's checking the filename and class name, but again, i'm not certain.

That's how i like to think of it. If anyone else can correct me or improve my comment, feel free.

1 like
learn@karmanya.co.in's avatar

By digging further what I had learnt is:

For the files which do not follow PSR-0/4 standards and still wanted to autoload then we can use classmaps.

For the files which do follow PSR-0/4 standards they can directly be autoloaded using composer by the rules set by PSR-0/4.

I think as migrations and other stuff in database does not follow PSR-4 standards so using classmaps. "Why?" not following PSR-4 standards (may be the file size of vendor/composer/autoload_classmap.php is increasing) can be better explained from these following link.

https://getcomposer.org/doc/articles/autoloader-optimization.md#autoloader-optimization

As laravel is configured to "optimize-autoload" after every composer install or update, we can use "exclude-from-classmap" for tests and database to not increase the size of autoload_classmap.php file size.

Why I'm so concerned about this issue is, I would like to develop a package for extending migrations. That's why I would like to understand base reason why it is like that, not the otherway around. Without understanding the reason, all my work would go for a toss.

Please correct me if there is something wrong in my explanation and suggest me anything regarding this issue to follow PSR-4 or not.

Please or to participate in this conversation.