Overriding Laravel’s Default Models Folder using Artisan Command
Tags: laravel, laraveldevelopment, laravelframework
Published: 2020-07-20 20:28:10

---
title: Overriding Laravel’s Default Models Folder using Artisan Command
published: true
date: 2020-07-17 12:55:57 UTC
tags: laravel,laraveldevelopment,laravelframework
canonical_url: https://medium.com/@elishaukpongson/overriding-laravels-default-models-folder-using-artisan-command-a362f5d77765
---

A few weeks ago, the creator of Laravel, Taylor Otwell made a poll asking the framework’s users how they arrange their model files in the app folder.

{% tweet 1277709898244141062 %}

The framework default stores all models in the app folder directly but most developers and me inclusive would prefer to have a specific models folder to house all models.

![](https://cdn-images-1.medium.com/max/820/1*FeKKecKWXvuMRBXZcSRBcw.png)<figcaption>Default Laravel Models arrangement.</figcaption>

![](https://cdn-images-1.medium.com/max/784/1*bvuDOwGl5Cu_7FRVB33SOw.png)<figcaption>My way of arranging models.</figcaption>

Following this method of arranging models comes with a bit of tediousness as the folder name has to be specified as a namespace when creating the model using the artisan command. To create a model the default way, you can run this command below in the terminal:

> php artisan make:model Bank

This command will create a new Bank model class and put the file directly in the app folder.

To create a model directly into any other folder, for me, the Models folder, this has to be specified when running the command, see below:

> php artisan make:model Models\\Bank

While doing this might not look like a big deal any time you want to create a new model, but having more than one developer on the project or creating a lot of models over time, you might tend to forget adding the _Models\\_ prefix every time and that will create the model into the default app folder, not what we want.

To fix this, we can extend the Laravel’s default model creation artisan command and edit the default namespace/folder so we do not have to prepend the namespace/folder name while creating a new model.

_Note: The folder name, is the namespace name too._

Firstly, create a new artisan command:

> php artisan make:command ModelMakeCommand

This creates a new command class/file in the app\Console\Commands folder with contents:

```
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ModelMakeCommand extends Command
{
_/\*\*  
     \* The name and signature of the console command.  
     \*  
     \*_ **_@var_** _string  
     \*/_ protected $signature = 'command:name';

_/\*\*  
     \* The console command description.  
     \*  
     \*_ **_@var_** _string  
     \*/_ protected $description = 'Command description';

_/\*\*  
     \* Create a new command instance.  
     \*  
     \*_ **_@return_** _void  
     \*/_ public function \_\_construct()
    {
        parent::_\_\_construct_();
    }

_/\*\*  
     \* Execute the console command.  
     \*  
     \*_ **_@return_** _int  
     \*/_ public function handle()
    {
        return 0;
    }
}
```

Secondly, we will change the class that our ModelMakeClass extends from _Illuminate\Console\Command_ to _Illuminate\Foundation\Console\ModelMakeCommand_

Thirdly, we will empty the ModelMakeClass of its default methods and create a new method called _getDefaultNamespace_ in the class to handle the task we want it to do (this method overrides the one in the _Illuminate\Foundation\Console\ModelMakeCommand we are extending_).

The ModelMakeClass becomes:

```
<?php

namespace App\Console\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand as Command;

class ModelMakeCommand extends _Command_  
{

_/\*\*  
     \* Get the default namespace for the class.  
     \*  
     \*_ **_@param_** _string $rootNamespace  
     \*_ **_@return_** _string  
     \*/_ protected function getDefaultNamespace($rootNamespace)
    {
        //logic to change default behaviour
    }

}
```

The final thing to add to the new command class we just created is the logic to make our folder the default folder to put model files, in my case the Models folder.

![](https://cdn-images-1.medium.com/max/1024/1*zS6JkDth1F-bWfJFblLUjQ.png)<figcaption>The two different versions of the getDefaultNamespace methods.</figcaption>

Our new command class now becomes:

```
<?php

namespace App\Console\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand as Command;

class ModelMakeCommand extends _Command_  
{
_/\*\*  
     \* Get the default namespace for the class.  
     \*  
     \*_ **_@param_** _string $rootNamespace  
     \*_ **_@return_** _string  
     \*/_ protected function getDefaultNamespace($rootNamespace)
    {
        return "{$rootNamespace}\Models";
    }
}
```

Finally, running the artisan command to create a new model will put the models in the _app\Models_ folder by default 🙂.

TL;DR:

To change the default model folder in a Laravel app

- Create a new artisan command with name ModelMakeCommand
- Extend the _Illuminate\Foundation\Console\ModelMakeCommand_ class instead of the _Illuminate\Console\Command_ class.
- Remove other methods and attributes from the class and then create a new method called _getDefaultNamespace_ with content:

```
_/\*\*  
     \* Get the default namespace for the class.  
     \*  
     \*_ **_@param_** _string $rootNamespace  
     \*_ **_@return_** _string  
     \*/_ protected function getDefaultNamespace($rootNamespace)
    {
        return "{$rootNamespace}\Models";
    }
```

- The full class becomes:

```
<?php

namespace App\Console\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand as Command;

class ModelMakeCommand extends _Command_  
{
_/\*\*  
     \* Get the default namespace for the class.  
     \*  
     \*_ **_@param_** _string $rootNamespace  
     \*_ **_@return_** _string  
     \*/_ protected function getDefaultNamespace($rootNamespace)
    {
        return "{$rootNamespace}\Models";
    }
}
```

- Run model creation artisan command and the models will be seen in the app\Models folder.

Thanks