Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 774
James Jeffery Avatar asked Jan 29 '15 21:01

James Jeffery


3 Answers

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-name\Models\ModelName

You can access this model in inside your controller

use app-name\Models\ModelName
like image 139
Mansoor Akhtar Avatar answered Oct 23 '22 16:10

Mansoor Akhtar


In Laravel 5.4 or later

You can create as below

 > php artisan make:model "Models\userModel"

here Models is directory name and userModel is model name

Use " (double quotes) or ' (single quotes) to create model

like image 26
Jazzzzzz Avatar answered Oct 23 '22 15:10

Jazzzzzz


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:

Laravel 5.2

php artisan model:make App\\Models\\Foo

Laravel 5.3

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.

Hope that helps.

like image 23
mmieluch Avatar answered Oct 23 '22 16:10

mmieluch