CrestApps / laravel-code-generator

An efficient Laravel code generator, saving time by automating the creation of resources such as views, controllers, routes, migrations, languages, and form-requests. Highly flexible and customizable, it includes a cross-browser compatible template and client-side validation for application modernization.
https://laravel-code-generator.crestapps.com
MIT License
728 stars 156 forks source link

Namespace models and from-database issue #77

Closed nenads closed 6 years ago

nenads commented 6 years ago

Environment:

Description:

When having models with more depth then just App\Models you cant create resource from existing database.

Steps/Commands To Reproduce:

php artisan resource-file:from-database "\App\Models\Test\Some" --table-name=tests --translation-for=en,sr

Content Of The Resource-File:

image

MikeAlhayek commented 6 years ago

try changing your command to this instead

php artisan resource-file:from-database Some --model-directory="App\Models\Test" --table-name=tests --translation-for=en,sr

nenads commented 6 years ago

The "--model-directory" option does not exist.

when i change in CrestApps\CodeGenerator\Traits\ModelTrait

public static function getModelNamespace($modelName, $modelDirectory = null)

It passes since this function expect two arguments.

MikeAlhayek commented 6 years ago

Ouch! I'll dig into the issue when time permits. I am glad your that you're testing v 2.3

MikeAlhayek commented 6 years ago

I just thought about something. The resource-file would only create the resource file, it has nothing to do with how the model is generated. Are you trying to create the resource-file in a sub-folder or you're just trying to create the model in a subfolder?

if you're just trying to create the model in a subfolder, then do the following to create the resource-file from the database

php artisan resource-file:from-database Some --table-name=tests --translation-for=en,sr

then you can create the model using the following command

php artisan create:model Some --model-directory="App\Models\Test" --table-name=tests --translation-for=en,sr

or you can create all the resources along with the resource file like so

php artisan create:scaffold Some --model-directory="App\Models\Test" --table-name=tests --table-exists --translation-for=en,sr

Let me know if that is what you're looking for or something else

MikeAlhayek commented 6 years ago

I am trying to understand if you're trying to create a resource file or a model. Please check my previous comments

nenads commented 6 years ago

Hi,

This is my second project with code generatpr. First time i used 2.2. Here is some retrospect about it. Btw it took me time to figure out best methodology to use it in my flow.

Everything that generator generates is for admin only exception is that generated models would be considered as base for its entity(table). Then copy generated stuff alter it for app usage. Nice consistency in code that is.

When i copy generated controllers, requests and views for users i altered them for design sometime just change auth guard or add some logic.

Generated models are in Base namespace. example would be App\Models\Base\Post. Then if needed i would create App\Models\User\Post that would extend Base\Post.

So what i did like: i could regenerate over and over while my project would evolve. Admin would have crud and my changes would stay or it would be easy to sync. Usually copy new filed to Custom views, requests.... Pain was: Maintaining resources and its fields . Due fact that you in best case can generate fields and then edit json files since options are not supported etc...

In the end i love project since you can change templates etc.... (btw do you have resource file with all possible variation of fields that are supported that would speed up when new templates need to be created. Upgrade to bootstrap 4 for example)

Second project: i know 2.2 and it is time to use not documented version 2.3 :)

For this one i decided to have same folder structure. Only difference would be that i would create migrations and base models manual. why?

Then use code generator to generate resources from database and use created models.

That is why i needed namespace models since base would be used for admin and extended to app.

What do you think of my methodology ? am i missing something any suggestion would be nice.

Regarding command and issue

php artisan create:scaffold Post --model-directory="App\Models\Base\Post" --table-name=posts --table-exists --translation-for=en,sr

image

It seems that when method is called second argument is missing.

nenads commented 6 years ago

What would be desired resource name for namespace models $this->getNewFilename();

https://github.com/CrestApps/laravel-code-generator/blob/v2.3/src/Commands/Resources/ResourceFileFromDatabaseCommand.php#L72

image

nenads commented 6 years ago

My conclusion is that resource-file:from-database is using old calls

image

MikeAlhayek commented 6 years ago

I fixed the bug that you ran into. Pull the latest should fix it.

I am having hard time following your method/workflow. Why do you need the code-generator to create a base class for the models? If for some reason you need to extend a different class than the Illuminate\Database\Eloquent\Model class, you should be able to pass --model-extends property with the full-name of the base model. for example `--model-extends="App\Models\Base\Post"

If you're admin look the same, you should not need to edit the code/views manually each time. You can be wasting a lot of time. Instead, you should create a new template for your admin work and use the admin_template when creating code. Your goal should be creating the code with almost no need to change the generated code manually. The controller/scaffold command gives you the --with-auth option which adds the proper code if your using Laravel-Passport for your authentication. If you're using other packages than laravel-passport, then you should be able to add the needed code in the admin_template directly so the generated code will be ready and no need to ever edit it. The idea here is to prevent you from having the need to edit files manually which will save you time and will allow you to regenerate the resources if you ever need to change the look/feel of your site without having the need to manually do the work.

If I missed something from your comments above, then please share some code example with me so I can help you better. Show me the commands you used, the table structure of your database, the generated code and how it looks after your edit. this will help me understand the changes.

MikeAlhayek commented 6 years ago

Is this still an issue?