定制 laratusk/view-migrations 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

laratusk/view-migrations

Composer 安装命令:

composer require laratusk/view-migrations

包简介

Manage database views via SQL files with automatic content-hash-based change detection

README 文档

README

Manage database views via plain SQL files with automatic change detection. The package computes a content hash for each view file and tracks it in Laravel's migrations table.

Supports MySQL, PostgreSQL, and SQLite.

Installation

composer require laratusk/view-migrations

Publish the config file:

php artisan vendor:publish --tag=view-migrations-config

How It Works

  1. You write plain SQL SELECT statements in database/views/*.sql
  2. The package computes a content hash for each file
  3. On view:migrate, it compares the file hash with the hash stored in the migrations table
  4. Views are created, updated, or skipped accordingly

The hash is stored in Laravel's migrations table as vm:{view_name}:{hash}, keeping the tracking mechanism consistent across all database drivers.

Quick Start

Create a view file:

php artisan view:make user_stats

Edit database/views/user_stats.sql:

SELECT
    user_id,
    COUNT(*) as total_orders,
    SUM(amount) as total_spent
FROM orders
GROUP BY user_id;

Apply it:

php artisan view:migrate

Commands

view:migrate

Syncs all SQL files to database views.

php artisan view:migrate
php artisan view:migrate --dry-run          # Preview changes without executing
php artisan view:migrate --drop-orphans     # Drop views with no matching SQL file

view:status

Shows the current state of all views.

php artisan view:status
+-------------+--------------+--------------+--------+
| View        | File Hash    | DB Hash      | Status |
+-------------+--------------+--------------+--------+
| user_stats  | a1b2c3d4e5f6 | 9f8e7d6c5b4a | Stale  |
| daily_sales | c3d4e5f6a1b2 | c3d4e5f6a1b2 | OK     |
| new_report  | e5f6a1b2c3d4 | —            | New    |
| old_view    | —            | 7d6c5b4a9f8e | Orphan |
+-------------+--------------+--------------+--------+

view:make {name}

Creates a new SQL file in database/views/.

php artisan view:make analytics_summary

Usage in Migrations

You can trigger view migrations from Laravel migrations:

use Laratusk\ViewMigrations\Facades\ViewMigrator;

return new class extends Migration
{
    public function up(): void
    {
        ViewMigrator::migrateView('analytics_summary');
    }

    public function down(): void
    {
        ViewMigrator::dropView('analytics_summary');
    }
};

Configuration

// config/view-migrations.php

return [
    // Directory where .sql view files are stored
    'path' => database_path('views'),

    // Only manage views matching this prefix (null = manage all)
    'prefix' => null,
];

Events

Event Properties
ViewCreated viewName, hash
ViewUpdated viewName, oldHash, newHash
ViewDropped viewName
ViewMigrationCompleted result (ViewMigrationResult DTO)

Testing

# SQLite (default)
vendor/bin/pest

# MySQL
DB_CONNECTION=mysql DB_DATABASE=testing DB_USERNAME=root DB_PASSWORD=secret vendor/bin/pest

# PostgreSQL
DB_CONNECTION=pgsql DB_DATABASE=testing DB_USERNAME=postgres DB_PASSWORD=secret vendor/bin/pest

License

MIT License. See LICENSE.md.

统计信息

  • 总下载量: 483
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固