Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
recognize unique values
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 4, 2019
1 parent cbb7523 commit 89fb470
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/Commands/PrefillFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,11 @@ public function handle()
}

$isForeignKey = $this->isForeignKey($column, $tableIndexes);
$isUnique = $this->isUnique($column, $tableIndexes);

$value = $isForeignKey
? $this->buildRelationFunction($modelClass, $data->name)
: '$faker->' . $this->mapToFaker($data);
: ($isUnique ? '$faker->unique()->' : '$faker->') . $this->mapToFaker($data);

return "'$data->name' => $value";
})->filter(function ($data) {
Expand Down Expand Up @@ -133,7 +134,21 @@ protected function mapToFaker($data)
protected function isForeignKey($name, $tableIndexes)
{
return !!array_where(array_keys($tableIndexes), function ($index) use ($name) {
return str_contains($index, $name);
return str_contains($index, 'foreign') && str_contains($index, $name);
});
}

/**
* Check if column is a unique one.
*
* @param string $name
* @param array $tableIndexes
* @return boolean
*/
protected function isUnique($name, $tableIndexes)
{
return !!array_where(array_keys($tableIndexes), function ($index) use ($name) {
return str_contains($index, 'unique') && str_contains($index, $name);
});
}

Expand Down

0 comments on commit 89fb470

Please sign in to comment.