Skip to content

Commit

Permalink
Display unique
Browse files Browse the repository at this point in the history
  • Loading branch information
jessarcher committed Jul 15, 2022
1 parent 9927025 commit e1a8ce1
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/Illuminate/Foundation/Console/ShowModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Foundation\Console;

use Doctrine\DBAL\Schema\Column;
use Doctrine\DBAL\Schema\Index;
use Doctrine\DBAL\Types\DecimalType;
use Illuminate\Console\Command;
use Illuminate\Contracts\Container\BindingResolutionException;
Expand Down Expand Up @@ -113,6 +114,7 @@ protected function getAttributes($model)
{
$schema = $model->getConnection()->getDoctrineSchemaManager();
$columns = $schema->listTableColumns($model->getConnection()->getTablePrefix().$model->getTable());
$indexes = $schema->listTableIndexes($model->getTable());

return collect($columns)
->values()
Expand All @@ -122,6 +124,7 @@ protected function getAttributes($model)
'increments' => $column->getAutoincrement(),
'nullable' => ! $column->getNotnull(),
'default' => $this->getColumnDefault($column, $model),
'unique' => $this->columnIsUnique($column->getName(), $indexes),
'fillable' => $model->isFillable($column->getName()),
'hidden' => $this->attributeIsHidden($column->getName(), $model),
'appended' => null,
Expand Down Expand Up @@ -162,6 +165,7 @@ protected function getVirtualAttributes($model, $columns)
'increments' => false,
'nullable' => null,
'default' => null,
'unique' => null,
'fillable' => $model->isFillable($name),
'hidden' => $this->attributeIsHidden($name, $model),
'appended' => $model->hasAppended($name),
Expand Down Expand Up @@ -271,7 +275,7 @@ protected function displayCli($class, $database, $table, $attributes, $relations
);

foreach ($attributes as $attribute) {
$first = sprintf('%s %s', $attribute['name'], collect(['increments', 'nullable', 'fillable', 'hidden', 'appended'])
$first = sprintf('%s %s', $attribute['name'], collect(['increments', 'unique', 'nullable', 'fillable', 'hidden', 'appended'])
->filter(fn ($property) => $attribute[$property])
->map(fn ($property) => sprintf('<fg=gray>%s</>', $property))
->implode('<fg=gray>,</> '));
Expand Down Expand Up @@ -394,6 +398,20 @@ protected function attributeIsHidden($attribute, $model)
return false;
}

/**
* Determine if the given attribute is unique.
*
* @param string $column
* @param \Doctrine\DBAL\Schema\Index[] $indexes
* @return bool
*/
protected function columnIsUnique($column, $indexes)
{
return collect($indexes)
->filter(fn (Index $index) => count($index->getColumns()) === 1 && $index->getColumns()[0] === $column)
->contains(fn (Index $index) => $index->isUnique());
}

/**
* Qualify the given model class base name.
*
Expand Down

0 comments on commit e1a8ce1

Please sign in to comment.