Skip to content

Commit

Permalink
[10.x] Determine if the given view exists. (#49231)
Browse files Browse the repository at this point in the history
* add `Schema::hasView()`

* override `Schema::hasTable()` on SQL Server

* add a test

* formatting

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
hafezdivandari and taylorotwell committed Dec 4, 2023
1 parent be526ee commit 484e9c2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Database/Schema/Builder.php
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Support/Facades/Schema.php
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/Integration/Database/SchemaBuilderTest.php
Expand Up @@ -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');
Expand Down

0 comments on commit 484e9c2

Please sign in to comment.