Skip to content

[9.x] Ignore whitespaces/newlines when finding relations in model:show command - #45608

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
binotaliu:bugfix/show-model-ignore-whitespaces
Jan 11, 2023
Merged

taylorotwell merged 1 commit into
laravel:9.xfrom
binotaliu:bugfix/show-model-ignore-whitespaces

Conversation

@binotaliu

@binotaliu binotaliu commented Jan 11, 2023 •

Copy link
Copy Markdown
Contributor

The show:model relies on reading methods' source code to finding relations. It's done by finding if the pattern \$this->{$possibleRelationMethod}( appears in the method.
In most situation, it would work:

public function user()
{
    return $this->belongsTo(User::class);
}

But if there are whitespaces/newlines after $this, the command would failed to detect:

public function user()
{
    return $this
        ->belongsTo(User::class)
        ->withDefault([
            'name' => 'Guest Author',
        ]);
}

1

This PR fixes this issue by trimming each line of the relation method's source code, so the pattern \$this->{$possibleRelationMethod}( can be correctly detected.

public function user(){return $this->belongsTo(User::class)->withDefault(['name' => 'Guest Author',]);}

Footnotes

  1. Example code from Laravel Documentation ↩

@taylorotwell
taylorotwell merged commit 32666a5 into laravel:9.x Jan 11, 2023
@binotaliu
binotaliu deleted the bugfix/show-model-ignore-whitespaces branch January 11, 2023 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants