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

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

Related

Do composer commands manually

Hello I want to connect a payment gateway in my laravel project. I can do that with composer but This script is located on the host and I do not have access to SSH.
Do you know how can I do these two commands manually?
composer require econea/nusoap:dev-master
php artisan make:controller siteController
Add the package to the vendor folder
Add a reference in \vendor\composer\autoload_namespaces.php
Add a reference in \vendor\composer\autoload_psr4.php
Work automatically now when the vendor\autoload.php is called
php artisan make:controller siteController
This will create a controller, The created can be seen at app/Http/Controllers.
Used for a create a new controller without a composer:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class siteController extends Controller
{
//
}

Laravel: How to generate seeders in a custom directory

I'm using Laravel 5.5 with laravel-modules v2.
It's easy to generate migrations in a custom directory (inside a module, specifically):
php artisan make:migration create_users_table --path=Modules/User/Database/Migrations
But seems that this isn't possible with seeding classes:
php artisan make:seeder UsersTableSeeder --path=Modules/User/Database/
The "--path" option does not exist.
or passing full relative path:
php artisan make:seeder Modules/User/Database/Migrations/UsersTableSeeder
Creates this exactly folder structure inside ./database/seeds/
or passing full absolute path:
php artisan make:seeder /Modules/User/Database/Migrations/UsersTableSeeder
file_put_contents(\my\file\system\project\database/seeds/C:/Program Files/Git
/Modules/User/Database/Seeders/UsersTableSeeder.php): failed to open str
eam: No such file or directory
How to generate seeders with artisan command in a custom directory?
You can't. The GeneratorCommand (which the Seeder extends) doesn't care about whether or not folders exist, because it's just going to write the file only.
/**
* Get the destination class path.
*
* #param string $name
* #return string
*/
protected function getPath($name)
{
return $this->laravel->databasePath().'/seeds/'.$name.'.php';
}
The only way to achieve what you want is to write your own Seeder command and allow for directory traversal. You can inspect the Illuminate\Database\Console\Migrations\MigrateMakeCommand to see how it's done, it's not very difficult.
You can't but you can do it with your custom code after do following steps:
Copy file from Illuminate/Database/Console/Seeds/SeederMakeCommand.php to your
folder.
Modify getPath() function.
Bind this class to container to override core.
I had same problems bcs in our team we want to create two folder, one for real data seeders and the other for fake data seeders.
in DatabaseSeeder file you can define your classes from another path ( namespace ) and it will work after running php artisan db:seed command
in my case I define new call :
$this->call([
//real data classes from \database\seeds path
]);
$this->call([
//fake data from database\seeds\tests path
'Database\Seeds\Tests\AuthControllerTestSeeder'
]);
do not forget to define your namespace (Database\Seeds\Tests\AuthControllerTestSeeder) in

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 can i rename laravel controller with command line interface(CLI)?

I created controller which name is Home but I want rename it with HomeController. Is it possible in laravel?
Example:
class Home extends Controller { }
I Want to rename it like
class HomeController extends Controller { }
I Want to do it with CLI.
You can't use artisan, but you can do it manually. But it's not just a simple rename.
You have to:
Rename the PHP file.
Edit the PHP file to give the class the new controller name (the class name and the file name must match)
Change any code that uses that old class name to use the new class name
Edit the autoload files to load the new controller file:
vendor/composer/autoload_static.php
vendor/composer/autoload_classmap.php
You have to do it manually, there is no any command available to updated controller name.
Make a new Controller with the new Name.
php artisan make:controller NewController
or
php artisan make:controller NewController --resource
//if it is a resource controller
Now copy all contents from Old Controller to NewController.
Then edit all routes (in web.php) to that Controller.
forget CLI why not try rename the controller using 'AS'
use app\MyController as MyControl;
I think doing composer dump-autoload this regenerates the files :
vendor/composer/autoload_static.php
vendor/composer/autoload_classmap.php
You can assess the file using nano command or vi, it depends on your machine. it basically an editor the enable you to change the content of the file.

Create model in a custom path in laravel

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.

Categories