How do I instruct artisan to save model to specific directory?

Multi tool use
How do I instruct artisan to save model to specific directory?
I'm using Laravel 5. I have created a /Models
directory under the /App
directory, but when generating the models using Artisan it's storing them under the App
directory.
/Models
/App
App
I have searched the documentation to try and find how to specify a different path name, but to no avail:
php artisan make:model TestModel
How do I instruct artisan
to save the model to specific directory?
artisan
Yes. There's 17 models in total, with various relationships, and the possibility of the models growing as more features are implemented. I do not want them under the
/app
directory. It's not the place for them anyway.– James Jeffery
Jan 29 '15 at 21:54
/app
My point is, won't it be fairly trivial to move them yourself?
– ceejayoz
Jan 29 '15 at 22:00
Oh, yeah sure that's what I've ended up doing but I'd still like to know if a solution exists as it's much quicker for my workflow to be able to use the generators.
– James Jeffery
Jan 29 '15 at 22:28
6 Answers
6
If you want to specify the path when generating a model, you can use the Laravel Generators Package. You can then specify the location using the --path
option like so:
--path
php artisan generate:model TestModel --path=my/custom/location
I'm using Laravel 5. That returns an error:
exception 'InvalidArgumentException' with message 'There are no commands defined in the "generate" namespace.' in /Users/me/wwwl/vendor/symfony/console/Symfony/Component/Console/Application.php:501
– James Jeffery
Jan 29 '15 at 22:29
exception 'InvalidArgumentException' with message 'There are no commands defined in the "generate" namespace.' in /Users/me/wwwl/vendor/symfony/console/Symfony/Component/Console/Application.php:501
And those are L4 generators not L5.
– James Jeffery
Jan 29 '15 at 22:30
There's version 3 of the generators which is for L5.
– Bogdan
Jan 29 '15 at 22:30
Just include
"way/generators": "~3.0"
in composer.json
. The instructions are here.– Bogdan
Jan 29 '15 at 22:31
"way/generators": "~3.0"
composer.json
I am getting Error
"--path" option does not exist
. using laravel 5.2– 151291
Oct 6 '16 at 10:12
"--path" option does not exist
Create a Models directory or whatever your want to named it, put it in inside app directory. The Directory structure should look like
laravel-project
/app
/Console
/Events
/Exceptions
/Http
/Jobs
/Listeners
/Provider
/Models
Then You just need to type artisan command for creating models inside Models directory
php artisan make:model Models/ModelName
After Creating Models your namespace inside model classes will be
namespace app-nameModelsModelName
You can access this model in inside your controller
use app-nameModelsModelName
For anyone wondering, this works perfectly. And this even creates the Models subdirectory if it doesn't exist.
– Víctor López
Oct 13 '15 at 4:58
Warning: Does not work in 5.2 with /App in your path. Data/Models/App/Phase will be put in Data/Models/ but with namespace Data/Models/App/Phase in file.
– Joeri
Mar 27 '16 at 16:30
For those using Laravel >= 5.2
It is possible to generate a model in a subdirectory using built-in Artisan generators by "escaping" the backslashes in the FQN, like so:
php artisan model:make App\Models\Foo
php artisan make:model App\Models\Foo
(the difference between 5.2 and 5.3 pointed out by @Khaled Rahman, thanks!)
Commands above would create Foo.php
file in the app/Models directory and update the namespace accordingly.
Foo.php
Hope that helps.
In laravel 5.3 :
php artisan make:model App\Models\Foo
– Khaled Rahman
Nov 10 '16 at 10:17
php artisan make:model App\Models\Foo
In laravel 5.4:
php artisan make:model Models/Foo
. It places the file under Models
directory with appropriate namespace.– Sisir
Jul 13 '17 at 20:43
php artisan make:model Models/Foo
Models
On linux (Laravel 5.3): php artisan model:make Models/Foo
– Marcelo Agimóvel
Oct 8 '17 at 17:45
In Laravel 5.4 you can create with
> php artisan make:model "ModelsuserModel"
here Models is directory name and userModel is model name
Use " (double quotes) or ' (single quotes) to create model
QUOTES solved the problem
– Husam
Jul 12 '17 at 10:09
Controller path (ApI/Admin)
Model path(Model/Admin)
php artisan make:controller API/Admin/PlanController --model=Model/Admin/Plan --resource
This works for actual Laravel version, 5.6.28
, on Windows 7
5.6.28
php artisan make:model AppModelsNewModel
Note: Do not use double escapes ('\'
)
'\'
This generate the file AppModelsNewModel.php
as follows
AppModelsNewModel.php
<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class NewModel extends Model
{
//
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you making models so often it's too much work to just move them where you want them?
– ceejayoz
Jan 29 '15 at 21:43