From 484e9c2c78637e3484681d7569a0e3f878dcd594 Mon Sep 17 00:00:00 2001 From: Hafez Divandari Date: Tue, 5 Dec 2023 01:56:42 +0330 Subject: [PATCH] [10.x] Determine if the given view exists. (#49231) * add `Schema::hasView()` * override `Schema::hasTable()` on SQL Server * add a test * formatting --------- Co-authored-by: Taylor Otwell --- src/Illuminate/Database/Schema/Builder.php | 19 +++++++++++++++++++ src/Illuminate/Support/Facades/Schema.php | 1 + .../Database/SchemaBuilderTest.php | 7 +++++++ 3 files changed, 27 insertions(+) diff --git a/src/Illuminate/Database/Schema/Builder.php b/src/Illuminate/Database/Schema/Builder.php index 92c5f76884ee..0807bdd76c17 100755 --- a/src/Illuminate/Database/Schema/Builder.php +++ b/src/Illuminate/Database/Schema/Builder.php @@ -168,6 +168,25 @@ public function hasTable($table) return false; } + /** + * Determine if the given view exists. + * + * @param string $view + * @return bool + */ + public function hasView($view) + { + $view = $this->connection->getTablePrefix().$view; + + foreach ($this->getViews() as $value) { + if (strtolower($view) === strtolower($value['name'])) { + return true; + } + } + + return false; + } + /** * Get the tables that belong to the database. * diff --git a/src/Illuminate/Support/Facades/Schema.php b/src/Illuminate/Support/Facades/Schema.php index ddece45e6e56..7a29296130eb 100755 --- a/src/Illuminate/Support/Facades/Schema.php +++ b/src/Illuminate/Support/Facades/Schema.php @@ -11,6 +11,7 @@ * @method static bool createDatabase(string $name) * @method static bool dropDatabaseIfExists(string $name) * @method static bool hasTable(string $table) + * @method static bool hasView(string $view) * @method static array getTables() * @method static array getViews() * @method static bool hasColumn(string $table, string $column) diff --git a/tests/Integration/Database/SchemaBuilderTest.php b/tests/Integration/Database/SchemaBuilderTest.php index 38f2c34d6cc8..a8ea8fbaba63 100644 --- a/tests/Integration/Database/SchemaBuilderTest.php +++ b/tests/Integration/Database/SchemaBuilderTest.php @@ -164,6 +164,13 @@ public function testGetTables() } } + public function testHasView() + { + DB::statement('create view foo (id) as select 1'); + + $this->assertTrue(Schema::hasView('foo')); + } + public function testGetViews() { DB::statement('create view foo (id) as select 1');