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

isimmons's avatar

artisan make:model specify the directory and namespace?

Just checking in case I'm missing something but the make:model command only accepts a single argument of class name and places it directly under app/

I prefer to have a directory for each model/entity under a Models or Entities directory such as app/Entities/User/User.php but it's not possible with the artisan command without cut/pasting it to the right place and then setting the namespace which kinda defeats the purpose of the artisan command.

Surely I'm missing something and am not the only one that doesn't want to fill up my app directory with a bunch of models :-)

0 likes
20 replies
khoanguyenme's avatar
Level 3

Very easy then php artisan make:model Entities\User\User

6 likes
isimmons's avatar

Have you tried this? I get

Entities
--User
----User.php

But look at the class

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Entities/User/User extends Model {

    //

}
michaeldyrynda's avatar

Looks like it uses your global namespace (set with artisan app:name) but there doesn't appear to be any way to pass in the namespace parameter to override it.

1 like
isimmons's avatar

Thanks guys but I don't want to override the global namespace. In this instance the namespace would be App but the classes namespace would be appended to it as in App\Entities\User

If it were possible,

artisan make:model Entities\User\User

Would create the following class

<?php namespace App\Entities\User;

use Illuminate\Database\Eloquent\Model;

class User extends Model {

    //

}

But apparently it is not possible. I can just use a model snippet in my editor instead.

michaeldyrynda's avatar

Actually, @isimmons, it does work.

php artisan make:model Entities\\User\\User should make a new model instance in app/Entities/User/User.php. You just need to escape the backslash characters.

<?php namespace {namespace}\Entities\User;

use Illuminate\Database\Eloquent\Model;

class User extends Model {

    //

}
19 likes
isimmons's avatar

@deringer actually escaping messes it up a different way (probably a windows issue)

<?php namespace App\Entities\\Foo;

use Illuminate\Database\Eloquent\Model;

class \Foo extends Model {

    //

}

But I think I must have done artisan make:model Entities/User/User with forward slashes because it works with back slashes. Silly me. Thanks :-)

@khoanguyenme I chose your answer because you actually answered it first. I just wasn't paying attention to the slashes. Thanks.

juttimaar's avatar

Actually it's much easier than that. Simply say: php artisan make:model Entities/User/User.

It's forward slash not back slash.

7 likes
mariordev's avatar

Escaping the backlash characters in the php artisan command works with Mac OS. The model is created in the specified path and namespaced correctly, as @deringer suggested.

patrickcurl's avatar

I have to chime in make sure that you're using "/" NOT "" -- as it does matter, at least on linux/ubuntu.

2 likes
meeshka's avatar

@patrickcurl Does php artisan make:model "BackOffice\User" work on linux/ubuntu? Just checking. It works fine on Mac.

mjlovefl's avatar

It will create the model file, the problem however is you need to define the table if you do this.

For: php artisan make:model "BackOffice\User"

Eloquent will be looking for a table named "users".

What I would expect is for Eloquent to look for a table named "back_office_users".

1 like
JackRobertson's avatar

Another option would be to override laravel's ModelMakeCommand if you're lazy like me and don't want to type your app's default namespace every time you make a model.

Just make a command new command like so:

<?php

namespace App\Console\Commands;

use Illuminate\Foundation\Console\ModelMakeCommand as Command;

class ModelMakeCommand extends Command
{
    protected function getDefaultNamespace($rootNamespace)
    {
        return "{$rootNamespace}\Entities";
    }
}

Then register it in the Kernel:

protected $commands = [
    Commands\ModelMakeCommand::class
];

Now when you run the make:model command it will default into the app/Entities directory.

7 likes
digitalredefined's avatar

Just wanted to provide feedback for @meeshka - you example using the quotes around the path works on osx as well. When I tried it without it was just appending the namespace together for one big ugly model name. Thanks!

1 like
meeshka's avatar

@mikepmtl You will have to manually override the table name inside the model. If there are many such models, you can even consider a Contract or Trait perhaps?

imajkumar's avatar

Its works for me php artisan make:model Models\Enterproise\Enterprise

Ryahn's avatar

This works perfectly! Using php artisan make:model "Models\{namehere}" does make the folder and file, with the proper namespace.

1 like
braed's avatar

For those that have read this thread and are still unsure as to if this works on Windows - it does if you use double quotes like so

php artisan make:model "Models\Post\Post"

2 likes
iaskakho's avatar

I am trying to use another directory not app lets say foo.

I want the result to be foo/Models/MyModel.php anyway to achieve this?

Please or to participate in this conversation.