public function createTable(string $tableName, $firstRow)
{
$this->createTableSafely($tableName, function ($table) use ($firstRow) {
// Add the "id" column if it doesn't already exist in the rows.
if ($this->incrementing && ! array_key_exists($this->primaryKey, $firstRow)) {
$table->increments($this->primaryKey);
}
foreach ($firstRow as $column => $value) {
switch (true) {
case is_int($value):
$type = 'integer';
break;
case is_numeric($value):
$type = 'float';
break;
case is_string($value):
$type = 'string';
break;
case is_object($value) && $value instanceof \DateTime:
$type = 'dateTime';
break;
default:
$type = 'string';
}
if ($column === $this->primaryKey && $type == 'integer') {
$table->increments($this->primaryKey);
continue;
}
$schema = $this->getSchema();
$type = $schema[$column] ?? $type;
$table->{$type}($column)->nullable();
}
if ($this->usesTimestamps() && (! in_array('updated_at', array_keys($firstRow)) || ! in_array('created_at', array_keys($firstRow)))) {
$table->timestamps();
}
$this->afterMigrate($table);
});
}