Create model in a custom path in laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Creating Models in Custom Paths in Laravel: Moving Beyond the Default Structure
As developers working with large applications, we often need more structure than Laravel’s default conventions provide. While the standard practice of placing Eloquent models inside the `app/Models` directory is convenient for small projects, complex applications benefit from organized, modular structures. This post dives into how you can effectively create and manage your Eloquent models in custom paths, moving beyond the default setup.
## The Default Behavior vs. Custom Requirements
When you use the standard Artisan command:
```bash
php artisan make:model Core
```
Laravel dutifully creates the model file inside the `app/Models` directory, resulting in a file path like `app/Models/Core.php`. This convention is intentional; it aligns perfectly with Laravel’s service container and namespace resolution system.
However, if your application architecture demands that models belonging to a specific domain (like a `Core` module) reside in a custom folder structure—perhaps within a dedicated `Modules` or `Data` directory—we need a strategy beyond just manually creating files.
## Strategy 1: Manual Creation with Proper Namespacing (The Practical Approach)
The most straightforward, and often safest, method is to create the directory structure manually and then ensure your custom model class uses the correct namespace relative to your application's root.
If you decide to place your models in a custom path, for instance, `app/Modules/Core/Models`, you must adjust how Laravel finds these classes through proper PHP namespaces.
**Step 1: Create the Directory Structure**
Manually create the desired folder structure within your `app` directory:
```bash
mkdir -p app/Modules/Core/Models
```
**Step 2: Create the Model File**
Create your model file inside that new location:
```bash
touch app/Modules/Core/Models/Core.php
```
**Step 3: Define the Namespace Correctly**
In this scenario, you must explicitly define the namespace at the top of your `Core.php` file so that it resolves correctly when interacting with Eloquent and other parts of your application.
```php
// app/Modules/Core/Models/Core.php
namespace App\Modules\Core\Models;
use Illuminate\Database\Eloquent\Model;
class Core extends Model
{
// ... model properties
}
```
While this method works perfectly for organization, it bypasses the convenience of Artisan commands for initial setup. For complex setups involving multiple packages or modular systems, ensuring proper autoloading is crucial, which ties directly into robust architectural design principles found in modern frameworks like Laravel (as discussed on [laravelcompany.com](https://laravelcompany.com)).
## Strategy 2: Advanced Approach – Utilizing Composer for Custom Structure
For truly large-scale applications where you want to treat these custom model structures as distinct, reusable components—similar to how packages are managed—the best practice involves leveraging Composer's PSR-4 autoloading mechanism. This allows external code (or Laravel itself) to locate classes based on their directory structure.
Instead of relying solely on Artisan for the file creation, a more advanced developer approach is to treat these custom model directories as separate PHP packages within your project.
1. **Structure the Project:** Place your custom models outside the standard `app` directory, perhaps in a dedicated `src/Models` folder, and ensure this folder is properly registered as an autoloaded namespace via your `composer.json` file.
2. **Service Providers:** You would then register a custom Service Provider that specifically tells Laravel how to resolve these custom model paths when it initializes the Eloquent configuration.
This strategy moves from simple file creation to architectural design, ensuring that your application remains scalable and adheres to SOLID principles. When building complex systems, understanding dependency injection and autoloading is key to maintaining clean code, which is fundamental to leveraging powerful tools like those offered by Laravel.
## Conclusion
While you cannot directly instruct `php artisan make:model` to create files in arbitrary subdirectories within `app/Models`, the solution lies in adopting a disciplined approach. For simple organization, manual creation with strict namespace management works fine. For complex, modular applications, integrating custom model paths into your Composer-based autoloading system is the superior long-term strategy. By mastering these structural techniques, you ensure that your Laravel application remains clean, maintainable, and adheres to high architectural standards.