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
/app
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
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
"way/generators": "~3.0"
composer.json
"--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 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.
php artisan make:model App\Models\Foo
php artisan make:model Models/Foo
Models
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
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