Skip to content

Commit

Permalink
Merge pull request laracademy#49 from daverogers/new-lambdas-and-read…
Browse files Browse the repository at this point in the history
…me-updates

New Lambdas and README Updates
  • Loading branch information
michael-mcmullen authored Jun 23, 2021
2 parents 7092283 + c05d64b commit 71e44bf
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 65 deletions.
116 changes: 91 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,32 @@ You can use this command to generate a single table, multiple tables or all of y
This command comes with a bunch of different options, please see below for each parameter

* --table=
* This parameter if filled in will generate a model for the given table.
* You can also pass in a list of tables using comma separated values.
* --all=
* If this flag is present, then the table command will be ignored.
* This will generate a model for **all** tables found in your database.
* _please note that this command will only ignore the `migrations` table and no model will be generate for it_
* _note has explicitly set to true in command line_
* This parameter if filled in will generate a model for the given table
* You can also pass in a list of tables using comma separated values
* --all=[_true|false (default)_]
* If this flag is present, then the table command will be ignored
* This will generate a model for **all** tables found in your database
* You can optionally specify a whitelist/blacklist in `config/modelfromtable.php`
* _Note: by default, this command will only ignore the `migrations` table_
* --connection=
* by default if this option is omitted then the generate will use the default connection found in `config/database.php`
* To specify a connection ensure that it exists in your `config/database.php` first.
* By default, if omitted, the default connection found in `config/database.php` will be used
* To specify a connection, first ensure that it exists in your `config/database.php`
* --folder=
* by default all models are store in your _app/_ directory. If you wish to store them in another place you can provide the relative path from your base laravel application.
* please see examples for more information
* By default, all models are store in your _app/_ directory. If you wish to store them in another place you can provide the relative path from your base laravel application.
* Alternatively, use a lambda in `config/modelfromtable.php` to dynamically specify folder path
* --namespace=
* by default all models will have the namespace of App
* you can change the namespace by adding this option
* --debug=
* this shows some more information while running
* _note has explicitly set to true in command line_
* --singular
* this will create a singular titled model
* --overwrite=
* overwrite model(s) if exists
* _note has explicitly set to true in command line_
* --timestamps=
* By default, all models will have the namespace of `App\Models`
* Alternatively, use a lambda in `config/modelfromtable.php` to dynamically specify namespace
* --debug=[_true|false (default)_]
* Shows some more information while running
* --singular=[_true|false (default)_]
* This will create a singular titled model, e.g. "Categories" -> "Category"
* --overwrite=[_true|false (default)_]
* Overwrite model file(s) if exists
* --timestamps=[_true|false (default)_]
* whether to timestamp or not
* _note has explicitly set to true in command line_


## Examples
## Examples (CLI)

### Generating a single table

Expand Down Expand Up @@ -108,6 +104,76 @@ php artisan generate:modelfromtable --connection=spark --all=true
php artisan generate:modelfromtable --table=user --folder=app\Models
```

## Configuration file for saving defaults, dynamic lambdas
A [config file](https://github.com/laracademy/generators/blob/master/config/modelfromtable.php) 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. For example: `'all' => true`. Then when you run `php artisan generate:modelfromtable` all tables will be generated by default. 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.

### Primary Key, using lamba (config only)
Some apps do not use the Laravel default of `id`, so say your table name prefixes the primary key...
```php
'primaryKey' => fn(string $tableName) => "{$tableName}_id",
```
Some legacy databases do not conform to a standard, so a more powerful example could be querying the primary key column directly...
```php
'primaryKey' => (function (string $tableName) {
$primaryKey = collect(DB::select(DB::raw("SHOW KEYS FROM {$tableName} WHERE Key_name = 'PRIMARY'")))->first();
return ($primaryKey) ? $primaryKey->Column_name : false;
}),
```

### Whitelist/Blacklist (config only)
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...
```php
'blacklist' => ['migrations'];
'whitelist' => ['user_address', 'system_*'];
```

### Filename, using lambda
Large databases sometimes use a pattern of prefixing for organization, which you can use to organize your model files through a lambda.
```php
'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._

### Folder path, using lambda
Using the last example, you can also organize the folder path using the prefix...
```php
'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'

### Namespace, using lambda
Using the last example, you would want to then generate a matching namespace to the file path
```php
'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'

### Delimiter (config only)
By default array values are delimited with a simple comma, but a common preference is to delimit with a newline as well.
```php
'delimiter' => ",\n" . str_repeat(' ', 8),
```
Result:
```php
class SomeModel extends Model
{
protected $fillable = [
'SomeField',
'AnotherField',
'YetAnotherField'
];
```

## License
ModelGen is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

Expand Down
6 changes: 5 additions & 1 deletion config/modelfromtable.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

'folder' => '',

'filename' => '',

'debug' => false,

'all' => false,
Expand All @@ -36,5 +38,7 @@

'whitelist' => [],

'delimiter' => ', '
'delimiter' => ', ',

'timestamps' => false
];
Loading

0 comments on commit 71e44bf

Please sign in to comment.