Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fixes for model:show command #43301

Merged
merged 3 commits into from
Jul 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/Illuminate/Foundation/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Types\DecimalType;
use Illuminate\Console\Command;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -201,16 +202,21 @@ protected function getRelations($model)
return collect($this->relationMethods)
->contains(fn ($relationMethod) => str_contains($code, '$this->'.$relationMethod.'('));
})
->values()
->map(function (ReflectionMethod $method) use ($model) {
$relation = $method->invoke($model);

if (! $relation instanceof Relation) {
return null;
}

return [
'name' => $method->getName(),
'type' => Str::afterLast(get_class($relation), '\\'),
'related' => get_class($relation->getRelated()),
];
});
})
->filter()
->values();
}

/**
Expand Down Expand Up @@ -345,10 +351,10 @@ protected function getCastType($column, $model)
*/
protected function getCastsWithDates($model)
{
return collect([
...collect($model->getDates())->flip()->map(fn () => 'datetime'),
...$model->getCasts(),
]);
return collect($model->getDates())
nunomaduro marked this conversation as resolved.
Show resolved Hide resolved
->flip()
->map(fn () => 'datetime')
->merge($model->getCasts());
}

/**
Expand Down Expand Up @@ -431,7 +437,7 @@ protected function columnIsUnique($column, $indexes)
*/
protected function qualifyModel(string $model)
{
if (class_exists($model)) {
if (str_contains($model, '\\') && class_exists($model)) {
return $model;
}

Expand Down