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');