laravel-code-generator icon indicating copy to clipboard operation
laravel-code-generator copied to clipboard

Command php artisan resource-file:create not working with --relations parameters

Open SulaimanBello opened this issue 4 years ago • 0 comments

Environment:

  • Laravel-Code-Generator Version: v2.4.4
  • Laravel Version: v8.64.0

Description:

Following the documentation, this command php artisan resource-file:create could not work with --relations parameters.

Steps:

php artisan resource-file:append Post --fields="name:another" --relations="name:comments;type:hasMany;field:title;params:App\Models\Comment|post_id|id"

Running the cammand above would generate

ErrorException

Undefined offset: 1

The culprit seems to be the line below

  at ...\crestapps\laravel-code-generator\src\Models\ForeignRelationship.php:490
    486▕             if (!str_contains($part, ':')) {
    487▕                 continue;
    488▕             }
    489▕ 
  ➜ 490▕             list($key, $value) = Str::split([':', '='], $part);
    491▕ 
    492▕             if (($isParams = in_array($key, ['params', 'param'])) || str_contains($value, '|')) {
    493▕                 $value = explode('|', $value);
    494▕

A quick dirty fix was changing the line to:

list($key, $value) = Str::split(':', $part);

SulaimanBello avatar Oct 14 '21 20:10 SulaimanBello