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 :-)
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.
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.
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 {
//
}
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.
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";
}
}
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!
@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?