Create model in a custom path in laravel - php

I want to create a model in a custom path. If i write the php artisan command like the following it will create model inside app.
php artisan make:model core
But i want to create the model file inside the custom made Models folder. How can i do it ?

Just define the path where you want to create model.
php artisan make:model <directory_name>/<model_name>
e.g.
php artisan make:model Models/core

You can do it easily with custom directory of your Models Folder (I assume Models Folder is sub folder of App).
Just use this command php artisan make:model Models/core then you will get your desire result.
Thanks.

Related

make model , controller ,seeder,factory,migration in custom folder

Is there any way to create model with all other classes in custom folder
when I try
php artisan make:model locations/City --all
I expect to create
controllers/locations/CitiesController
models/locations/City
etc.
only create a model in a custom folder (locations)
but the controller and others in the main folder
This feature exists in Laravel 8 and Laravel 9. If you're using any of the earlier versions, it won't work.

Laravel artisan make:component outside resources folder

When you run php artisan make:component Foo it will always be created inside (root)/resources/views/component.
How can I specify its "parent directory" or any specific directory outside resources folder?
I mean, like creating it at (root)/custom-package/components.
Is there any existing options does make:component command has? or I'm thinking few ways how to achieve this:
Create a custom command that will create the php and blade files to the directory I want.
Create a new command and extend the Laravel's make:component command to modify its path. (Currently, I can't find yet the Class where I can extend it.)
Or is there a config where I can set it?
maybe you can use:
php artisan make:component Foo --path=your/custom/location
hope this help

Import Custom made libraries or templates with terminal just like Laravel Artisan make Command

We use laravel artisan make commands to create class, model, migration file. Make: auth imports bunch of authentication system files in the project directory.
Is it possible to create html-php template and import them to our project from command line or terminal. How to do it?
Example:
import admin.dashboard - ( Imports custom made admin dashboard with all files)
Artisan does not provides any html/view template and if want you can create an artisan command.
You can do something like this:
Create artisan command
php artisan make:command HtmlCommand
Create stub files(template file) with variable placeholder which will be replaced by artisan command options and generate html from it and save to view.

Make Model, Migration and Controller with one artisan command, but Controller needs to be in different directory

Is there a way to make Model, Migration and Controller with one Artisan command.
But the controller I would like to be in placed in different sub-directory, not in Artisan's default directory.
Thank you for help/suggestions.
In two lines you could do:
//create model and migration
php artisan make:model ModelName -m
//create controller in subfolder
php artisan make:controller subfolder/Controller

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?
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
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
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.
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:
php artisan generate:model TestModel --path=my/custom/location
You can override the default Laravel command with this console class
<?php
namespace App\Console\Commands;
use Illuminate\Foundation\Console\ModelMakeCommand;
class ModelMake extends ModelMakeCommand
{
protected function getDefaultNamespace($rootNamespace)
{
return $rootNamespace.'\\Models';
}
}
The next time you make a model
php artisan make:model Example
your class will be placed in
App\Models\Example.php
Edit: Laravel 8 natively support this. I copied this snippet from somewhere, I just can't remember where to credit.
You can simply use this (for Laravel 5.3+):
php artisan make:model your_path/model_name
If you don't specify a path it will use main 'app' folder as root. So you can navigate from there.
Models/Folder1/MyModel means app->Models->Folder1->MyModel.php
This works for actual Laravel version, 5.6.28, on Windows 7
php artisan make:model App\Models\NewModel
Note: Do not use double escapes ('\\')
This generate the file App\Models\NewModel.php as follows
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class NewModel extends Model
{
//
}
In Laravel 8+ with default Models directory it can be achieved like bellow.
php artisan make:model App\Modules\ModuleName\Models\ModelName
It generates model file in directory
app/Modules/ModuleName/Models/ModelName.php
<?php
namespace App\Modules\ModuleName\Models;
use Illuminate\Database\Eloquent\Model;
class ModelName extends Model {
...
}
}
Add this line in composer.json file if it is not defined.
"autoload": {
"psr-4": {
"App\\": "app/",
//...
}
Then in your terminal do;
php artisan make:model Models/your_path/model_name
Controller path (ApI/Admin)
Model path(Model/Admin)
php artisan make:controller API/Admin/PlanController --model=Model/Admin/Plan --resource

Categories