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] Suggest a different layout for the model:show command #43170

Closed
Show file tree
Hide file tree
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
44 changes: 0 additions & 44 deletions src/Illuminate/Console/View/Components/Table.php

This file was deleted.

118 changes: 38 additions & 80 deletions src/Illuminate/Foundation/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,14 @@
namespace Illuminate\Foundation\Console;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Types\DecimalType;
use Illuminate\Console\Command;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use ReflectionClass;
use ReflectionMethod;
use SplFileObject;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Helper\TableCell;

#[AsCommand(name: 'model:show')]
class ShowModelCommand extends Command
Expand Down Expand Up @@ -81,14 +77,13 @@ public function handle()
{
if (! interface_exists('Doctrine\DBAL\Driver')) {
return $this->components->error(
'To display model information, you must install the `doctrine/dbal` package using the Composer package manager.'
'Displaying model information requires [doctrine/dbal].'
);
}

$class = $this->qualifyModel($this->argument('model'));

try {
/** @var Model */
$model = $this->laravel->make($class);
} catch (BindingResolutionException $e) {
return $this->components->error($e->getMessage());
Expand Down Expand Up @@ -124,7 +119,6 @@ protected function getAttributes($model)
'name' => $column->getName(),
'type' => $this->getColumnType($column),
'nullable' => ! $column->getNotnull(),
'default' => $this->getColumnDefault($column, $model),
'fillable' => $model->isFillable($column->getName()),
'hidden' => $this->attributeIsHidden($column->getName(), $model),
'appended' => null,
Expand All @@ -151,9 +145,9 @@ protected function getVirtualAttributes($model, $columns)
)
->mapWithKeys(function (ReflectionMethod $method) use ($model) {
if (preg_match('/^get(.*)Attribute$/', $method->getName(), $matches) === 1) {
return [Str::snake($matches[1]) => 'Accessor'];
return [Str::snake($matches[1]) => 'accessor'];
} elseif ($model->hasAttributeMutator($method->getName())) {
return [Str::snake($method->getName()) => 'Attribute'];
return [Str::snake($method->getName()) => 'attribute'];
} else {
return [];
}
Expand All @@ -163,7 +157,6 @@ protected function getVirtualAttributes($model, $columns)
'name' => $name,
'type' => null,
'nullable' => null,
'default' => null,
'fillable' => $model->isFillable($name),
'hidden' => $this->attributeIsHidden($name, $model),
'appended' => $model->hasAppended($name),
Expand Down Expand Up @@ -259,57 +252,47 @@ protected function displayJson($class, $database, $table, $attributes, $relation
*/
protected function displayCli($class, $database, $table, $attributes, $relations)
{
$this->newLine();

$this->components->twoColumnDetail('<fg=green;options=bold>'.$class.'</>');
$this->components->twoColumnDetail('Database', $database);
$this->components->twoColumnDetail('Table', $table);

$this->newLine();

$this->components->table(
[
'Attribute',
'Type',
'Nullable',
'Default',
'Fillable',
'Hidden',
'Appended',
'Cast',
],
[
...$attributes->map(fn ($attribute) => collect($attribute)
->map(fn ($property) => $this->formatForCli($property))
->all()
),
[''],
[
'<fg=green;options=bold>Relation</>',
'<fg=green;options=bold>Type</>',
new TableCell('<fg=green;options=bold>Related</>', ['colspan' => 6]),
],
...$relations->map(fn ($relation) => [
$relation['name'],
$relation['type'],
new TableCell($relation['related'], ['colspan' => 6]),
]),
]
$this->components->twoColumnDetail(
'<fg=green;options=bold>Attributes</>',
'type <fg=gray>/</> <fg=yellow;options=bold>cast</>',
);
}

/**
* Format a value for the CLI.
*
* @param bool|null|string $value
* @return string
*/
protected function formatForCli($value)
{
return match ($value) {
true => 'Yes',
false => 'No',
null => '<fg=gray>-</>',
default => $value,
};
foreach ($attributes as $attribute) {
$first = sprintf('%s %s', $attribute['name'], collect(['nullable', 'fillable', 'hidden', 'appended'])
->filter(fn ($property) => $attribute[$property])
->map(fn ($property) => sprintf('<fg=gray>%s</>', $property))
->implode('<fg=gray>,</> '));

$second = collect([
$attribute['type'],
$attribute['cast'] ? '<fg=yellow;options=bold>'.$attribute['cast'].'</>' : null,
])->filter()->implode(' <fg=gray>/</> ');

$this->components->twoColumnDetail(
str($first)->trim(), $second,
);
}

$this->newLine();

$this->components->twoColumnDetail('<fg=green;options=bold>Relations</>');

foreach ($relations as $relation) {
$this->components->twoColumnDetail(
$relation['name'].' <fg=gray>'.$relation['type'].'</>',
$relation['related']
);
}

$this->newLine();
}

/**
Expand All @@ -322,11 +305,11 @@ protected function formatForCli($value)
protected function getCastType($column, $model)
{
if ($model->hasGetMutator($column) || $model->hasSetMutator($column)) {
return 'Accessor';
return 'accessor';
}

if ($model->hasAttributeMutator($column)) {
return 'Attribute';
return 'attribute';
}

return $this->getCastsWithDates($model)->get($column) ?? null;
Expand Down Expand Up @@ -356,36 +339,11 @@ protected function getColumnType($column)
{
$name = $column->getType()->getName();

$details = match (get_class($column->getType())) {
DecimalType::class => $column->getPrecision().','.$column->getScale(),
default => $column->getLength(),
};

$unsigned = $column->getUnsigned() ? ' unsigned' : '';

if ($details) {
return sprintf('%s(%s)%s', $name, $details, $unsigned);
}

return sprintf('%s%s', $name, $unsigned);
}

/**
* Get the default value for the given column.
*
* @param \Doctrine\DBAL\Schema\Column $column
* @param \Illuminate\Database\Eloquent\Model $model
* @return string|null
*/
protected function getColumnDefault($column, $model)
{
if ($column->getAutoincrement()) {
return 'increments';
}

return $model->getAttributes()[$column->getName()] ?? $column->getDefault();
}

/**
* Determine if the given attribute is hidden.
*
Expand Down