Laravel 5 Models Folder Configuration
In a standard Laravel application, the php artisan make:model command defaults to placing new model classes in the app/Models directory (in later versions) or app/models (depending on your specific version and migration history). If you are maintaining a legacy codebase or have a custom convention where models reside in app/models, the default behavior of newer Laravel versions might conflict with your established structure. While this is often a non-issue if you simply ignore the generated file, moving it to a specific location can help keep your project organized, especially when working with teams that adhere to strict folder conventions.
To change where the generated class lands, you need to understand that artisan does not have a single global configuration flag for this behavior. Instead, the output path is determined by the service provider's configuration regarding model generation. In Laravel 5, specifically version 5.1 and later, the most direct way to control this without modifying the artisan command itself involves updating your application's service configuration.
The primary mechanism controlling this behavior is found in the config/app.php file, but more critically, it relies on how the Illuminate\Console\Scheduling\Schedule or custom commands are registered. However, for standard model generation, the path is actually hardcoded in the MakeModel command class itself. To redirect the output, you must override the default behavior by creating a custom Artisan command.
Here is how you can achieve this cleanly:
Create a new command using the generator:
php artisan make:command MakeModelsInCustomPathLocate the generated file (e.g.,
app/Console/Commands/MakeModelsInCustomPath.php) and modify thehandlemethod. You will need to replace the default call toModel::make()with your own logic that specifies the target directory.The key is utilizing the
Illuminate\Support\Facades\Filehelper or directly manipulating the path returned by the factory. Here is a conceptual example of what the command logic should look like:use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Model; class MakeModelsInCustomPath extends Command { protected $signature = 'make:model {name}'; protected $description = 'Create a new model in your custom directory'; public function handle() { // Get the base path where you want models to live, e.g., app/models $basePath = $this->laravel->basePath('app/models'); // The standard MakeModel command logic usually expects 'app/Models' or 'app' // You can override the destination by adjusting the file creation logic. // Note: In Laravel 5, you often have to manually call the factory method // with a custom path if the default constant is set to 'app/Models'. return parent::handle(); // Or implement custom file writing logic here } }
However, there is a much simpler approach that avoids creating a new command entirely. In Laravel 5.1+, you can simply define your own MakeModel command or modify the existing one by extending it and overriding the destination path constant.
Even better, if you are not constrained by the need to use the exact php artisan make:model signature, you can configure your project to accept models in a custom directory by updating the app/Models constant in your service provider or by creating a custom alias command.
For instance, if you want the generated file to appear in app/models instead of app/Models, you can create a custom artisan command that extends Illuminate\Database\Console\Stubs\ModelStub. But wait, the stubs are static files; they don't care about the destination folder unless the code explicitly writes there.
The most robust solution without rewriting the entire command is to update your MakeModel command's logic to point to your desired directory. If you prefer to keep the default command behavior and just want the files in a different spot, you can create a symlink or adjust your autoloading rules, but that violates the "generation location" requirement.
Therefore, creating a custom command is the cleanest architectural decision. You define the signature exactly as the original command, but inside the handle method, you construct the full path to your preferred directory (e.g., app/models) and ensure the file is created there using File::put. This ensures that every time you run the command, it respects your folder convention without requiring manual file movement afterward.
It is worth noting that if you are using Laravel 5.2 or higher, the default namespace for models might also need to be adjusted in config/app.php to match your custom directory structure if you rely on autoloading conventions. Specifically, ensure your APP_PATH and the model namespace align with your physical directory layout.
By implementing a custom command, you maintain consistency across your team's workflow. No one needs to remember to move files after generation; the tool itself enforces the convention. This reduces cognitive load and minimizes the risk of accidentally creating models in the wrong place during rapid development cycles.