staudenmeir / laravel-migration-views by staudenmeir

Laravel database migrations with SQL views
845,689
200
5
Package Data
Maintainer Username: staudenmeir
Maintainer Contact: mail@jonas-staudenmeir.de (Jonas Staudenmeir)
Package Create Date: 2019-06-15
Package Last Update: 2024-12-15
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2025-02-06 15:03:10
Package Statistics
Total Downloads: 845,689
Monthly Downloads: 50,843
Daily Downloads: 2,425
Total Stars: 200
Total Watchers: 5
Total Forks: 17
Total Open Issues: 1

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version Total Downloads License

Introduction

This Laravel extension adds support for SQL views in database migrations.

Supports Laravel 5.5.25+.

Installation

composer require staudenmeir/laravel-migration-views:"^1.0"

Usage

Creating Views

Use createView() to create a view and provide a query builder instance or an SQL string:

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

$query = DB::table('users')->where('active', true);

Schema::createView('active_users', $query);

You can provide the view's columns as the third argument:

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

$query = 'select id from users where active = 1';

Schema::createView('active_users', $query, ['key']);

Use createOrReplaceView() to create a view or replace the existing one:

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

$query = DB::table('users')->where('active', true); 

Schema::createOrReplaceView('active_users', $query);

Renaming Views

Use renameView() to rename a view:

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

Schema::renameView('active_users', 'users_active');

Dropping Views

Use dropView() or dropViewIfExists() to drop a view:

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

Schema::dropView('active_users');

Schema::dropViewIfExists('active_users');

If you are using php artisan migrate:fresh, you can drop all views with --drop-views (Laravel 5.6.26+).

Checking For View Existence

Use hasView() to check whether a view exists:

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

if (Schema::hasView('active_users')) {
    //
}

Listing View Columns

Use getViewColumnListing() to get the column listing for a view:

use Staudenmeir\LaravelMigrationViews\Facades\Schema;

$columns = Schema::getViewColumnListing('active_users');

Contributing

Please see CONTRIBUTING and CODE OF CONDUCT for details.