Last active
May 28, 2023 20:50
-
-
Save jwhulette/82438df97ad418ef87f21752af14285c to your computer and use it in GitHub Desktop.
[Check if column exists] #php #laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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