mhmod dallal's avatar

make:model --all with custom\path

is there is a way to use the custom path in "php artisan make:model --all custom\path" for all the added files?

0 likes
4 replies
illuminatixs's avatar

Hi there,

You can simply write the path before the name of the model. Ex: php artisan make:model Some\\Path\\MyModel

1 like
mhmod dallal's avatar

@illuminatixs I want the controller also to follow the custom path not only the model like how factory create the factory classes also follow the custom path

illuminatixs's avatar
Level 4

@mhmod dallal Unfortunately the --all flag doesn't take the given path into account while executing the other file makes, you might be able to open a suggestion with the framework for a change in this behaviour.

For now: You should call each make method individually with the earlier given solution.. Or create a custom command that can handle this for you.

1 like
projectasphaliea's avatar

Why I need this

I don't know if you still need it, but I was looking into a solution today since I am using Laravel with Inertia and Vue and wanted a custom solution.

Basically what I wanted to do is automatically generate all the files such as Requests into sub folders of their parent model name, create the Resource/Collection files, but also generate all my basic front-end Vue components (These are admin only views to pretty straight forward).

So instead of

App/Requests/StoreCustomerRequest
App/Requests/UpdateCustomerRequest
App/Requests/StoreProductRequest
App/Requests/UpdateProductRequest

It should becomes

App/Requests/Customer/StoreCustomerRequest
App/Requests/Customer/UpdateCustomerRequest

App/Requests/Product/StoreProductRequest
App/Requests/Product/UpdateProductRequest

I myself find this a bit cleaner and especially when my project gets larger. And my goal was to also add Resources to this as well using the same sub-folder structure. E.g:

App/Http/Resources/Customer/CustomerCollection
App/Http/Resources/Customer/CustomerResource

App/Http/Resources/Product/ProductCollection
App/Http/Resources/Product/ProductResource

Solution

What I decided to do is just create a new custom command

php artisan make:command CustomInertiaModelCommand

I then decided to jump into the source code of Laravel to find the original code of the make:model command. Found the following on Github: https://github.com/laravel/framework/blob/10.x/src/Illuminate/Foundation/Console/ModelMakeCommand.php

I just copied this code 1:1 into my CustomInertiaModelCommand class, changed everything to my needs and added custom methods such as the one creating my default Vue3 dashboard pages for the model.

Then I also needed to create a custom command and copied over the code for RequestMakeCommand https://github.com/laravel/framework/blob/10.x/src/Illuminate/Foundation/Console/RequestMakeCommand.php

You just need to grab the existing commands and overwrite them to your needs as a new command class.

I haven't tried yet so I don't know if it works in a Command but maybe it's easier to extend the original command e.g:

class CusomModelMakeCommand extends ModelMakeCommand

That way you don't need all the unnecessary/duplicate functions and you can only overwrite the things you need or want to add.

1 like

Please or to participate in this conversation.