> */ private array $columns = []; /** @var array */ private array $tables = []; public function __construct(private string $connection = 'asteria') {} public function hasTable(string $table): bool { return $this->tables[$table] ??= Schema::connection($this->connection)->hasTable($table); } /** * @param array $candidates * @return array */ public function existingColumns(string $table, array $candidates): array { if (! $this->hasTable($table)) { return []; } $listing = array_fill_keys(array_map('strtolower', $this->columnListing($table)), true); return array_values(array_filter( $candidates, fn (string $column) => isset($listing[strtolower($column)]) )); } /** * @return array */ public function columnListing(string $table): array { if (isset($this->columns[$table])) { return $this->columns[$table]; } $connection = DB::connection($this->connection); if ($connection->getDriverName() === 'mysql') { $wrapped = $connection->getQueryGrammar()->wrapTable($table); $rows = $connection->select('show columns from '.$wrapped); $names = []; foreach ($rows as $row) { $row = (array) $row; $name = $row['Field'] ?? $row['field'] ?? null; if (is_string($name) && $name !== '') { $names[] = $name; } } return $this->columns[$table] = $names; } return $this->columns[$table] = Schema::connection($this->connection)->getColumnListing($table); } }