Skip to content

Instantly share code, notes, and snippets.

@jwhulette
Last active May 28, 2023 20:50
Show Gist options
  • Save jwhulette/82438df97ad418ef87f21752af14285c to your computer and use it in GitHub Desktop.
Save jwhulette/82438df97ad418ef87f21752af14285c to your computer and use it in GitHub Desktop.
[Check if column exists] #php #laravel
<?php
declare(strict_types=1);
namespace Database\Helpers;
use Illuminate\Support\Facades\Schema;
class Column
{
public static function exists(string $table, string $column, ?string $database = null): bool
{
$columns = Schema::getConnection()->getDoctrineSchemaManager()->listTableColumns($table, $database);
$columnNames = collect($columns)->map(fn ($column) => $column->getName());
return $columnNames->contains($column);
}
}
// create helper folder in database folder
// add "Database\\Helpers\\": "database/helpers/" to composer psr-4 section
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment