Open skills-up opened 5 years ago
It’s been a while since I used it, but please tryphp artisan create:mapped-resources
.
The mapped-resources command is designed to allow you to create multiple resources all specified at once.
If you created resources using this package, most likely you already have resources/laravel-code-generator/sources/resources_map.json
created. Update that file directly by adding resources to it. Once you add a new entry for each resource you want to create, call php artisan create:mapped-resources
and all the resources should get created for you.
This command could be improved in the future to allow you to generate resource for every table on a given database. But in most cases, you’ll never want a CRUD for every table which is why the mapped-resource command allow you to only add what you really need.
You may also want to review the documentation on how to correctly modify the map file https://crestapps.com/laravel-code-generator/docs/2.2 The documentation has two related sections, once talks about the command and the second talk about the file’s format and what commands available there
I hope this helps.
PS: kindly use stackoverflow.com for question where github is for bugs or new future requests.
@skills-up this is what I've used.
First, run SHOW TABLES;
to get a list of all tables in your DB
Then map each table name to its model name in an array. Loop through the array and run the resource-file:from-database
with each set of values.
$models = [ "tableName" => "ModelName" ];
array_walk($models, function ($model, $table) {
$this->call('resource-file:from-database', [
'model-name' => $model,
'--table-name' => $table
]);
});
This will create all the necessary resource files and you can carry on from there.
NB: You can run the above code within Tinker.
@CrestApps what do you think about the approach I suggested here https://github.com/CrestApps/laravel-code-generator/issues/120#issuecomment-562066677 ?
I can submit a PR to add it as an option to the package.
@dbilovd the problem with that approach is SHOW TABLES
is mysql specific command. It won't work with other database servers.
I suggest creating a new command called resource-file:from-database-all
with no required parameters. Then I would clone the resource-file:from-database command but eliminate the model-name
as parameter and --database-name
, --resource-filename
as options.
That code in the ResourceFileFromdatabaseCommand.php
has a method called getParser() which gives you parser for the connected-to database. The parser should be modified to expose a method that would return an array of all tables found on the database. To do that, you'll need to add a new abstract method on the ParserBase.php class abstract protected function getTableNames();
Then in the MySqlParser you'll implement the getTableNames()
method which returns the result of SHOW TABLES
command.
If you do that, please submit a PR request.
@dbilovd you can even make the getModelName($tableName) public and then call $parser->getModelName("table-name")
within your new command to get the model name following the default naming convention.
Hi,
Is there a way to generate all the resources from an existing database?
I know we can generate resources pertaining to individual tables using:
php artisan resource-file:from-database [model-name]
What I wish to know is if there is any command like:
php artisan resource-file:from-database --all-tables
to generate resources for all the tables in the database with a single command?This will be really helpful for generating one click demos/applications from a schema, without the need to write a separate script to iterate through the database, and generating corresponding commands in the shell.
Thanks, Gaurav