This package will generate a Laravel Model based on your database table itself, filling in the required fields automatically.
Laracademy Generators - is a tool set that helps speed up the development process of a Laravel application., (*2)
Author(s): * Laracademy (@laracademy, michael@laracademy.co), (*3)
** For Laravel 5.* please use the version 1.5, (*4)
composer require "laracademy/generators" --dev
Now that we have added the generator to our project the last thing to do is run Laravel's Arisan command, (*5)
php artisan
You will see the following in the list, (*6)
generate:modelfromtable
This command will read your database table and generate a model based on that table structure. The fillable fields, casts, dates and even namespacing will be filled in automatically., (*7)
You can use this command to generate a single table, multiple tables or all of your tables at once., (*8)
This command comes with a bunch of different options, please see below for each parameter, (*9)
config/modelfromtable.php
migrations
table will be blacklisted by defaultconfig/database.php
will be usedconfig/database.php
config/modelfromtable.php
to dynamically specify folder pathApp\Models
config/modelfromtable.php
to dynamically specify namespacephp artisan generate:modelfromtable --table=users
php artisan generate:modelfromtable --table=users,posts
database.php
php artisan generate:modelfromtable --connection=spark
php artisan generate:modelfromtable --table=user --folder=app/Models
A config file should be in your project's config folder (if not, you can easily create it). Through this, you can set defaults you commonly use to cut down on the input your command line call requires. Some fields, like namespace
, accept a static value or, more powerfully, a lambda to generate dynamic values. Additional fields not available to the CLI are available in the config. See below., (*10)
Particularly large databases often have a number of tables that aren't meant to have models. These can easily be filtered through either the whitelist or blacklist (or both!). Laravel's "migrations" table is already included in the blacklist. One nice feature is that you can wildcard table names if that makes sense for your situation..., (*11)
'blacklist' => ['migrations']; 'whitelist' => ['user_address', 'system_*'];
Large databases sometimes use a pattern of prefixing for organization, which you can use to organize your model files through a lambda., (*12)
'filename' => fn(string $tableName) => Str::studly(Str::after($tableName, '_')),
In this example, 'system_user' would generate the filename 'User'. Note that this is also available through the CLI, but it probably doesn't make as much sense to set there., (*13)
Using the last example, you can also organize the folder path using the prefix..., (*14)
'folder' => (function (string $tableName) { $prefix = Str::studly(Str::before($tableName, '_')); $path = app()->path('Models') . "/{$prefix}"; if (!is_dir($path)) { mkdir($path); } return $path; }),
In this example, 'system_user' would generate the folder path 'path/to/your/install/app/Models/System', (*15)
Using the last example, you would want to then generate a matching namespace to the file path, (*16)
'namespace' => fn(string $folderpath) => 'App/Models' . Str::after($folderpath, app()->path('Models')),
Therefore the folder path 'path/to/your/install/app/Models/System' would generate the namespace 'App\Models\System', (*17)
By default array values are delimited with a simple comma, but a common preference is to delimit with a newline as well., (*18)
'delimiter' => ",\n" . str_repeat(' ', 8),
Result:, (*19)
class SomeModel extends Model { protected $fillable = [ 'SomeField', 'AnotherField', 'YetAnotherField' ];
ModelGen is open-sourced software licensed under the MIT license, (*20)
Please add as many details as possible regarding submission of issues and feature requests, (*21)
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE., (*22)