jwz104/eloquent-view

此包已被废弃,不再维护。未建议替代包。

使用eloquent创建sql视图

1.0.0 2017-09-17 19:09 UTC

This package is not auto-updated.

Last update: 2023-12-09 23:12:53 UTC


README

使用Eloquent view,您可以使用eloquent查询构建器创建一个SQL视图。这将防止在迁移中出现巨大的SQL字符串。

安装

运行composer require jwz104/eloquent-view

将服务提供者添加到config/app.php

'providers' => [
    Jwz104\EloquentView\EloquentViewServiceProvider::class,
]

可选地添加外观

'aliases' => [
    'EloquentView' => Jwz104\EloquentView\Facades\EloquentView::class,
]

如何使用

Eloquent view非常易于使用。
只需将构建器实例解析到视图构建器的create方法中。

示例迁移

class CreateEmployeesView extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $builder = DB::table('employees')
            ->join('companies', 'employees.company_id', '=', 'companies.id')
            ->select('employees.*', 'companies.name');

        EloquentView::create('employees_view', $builder);
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        EloquentView::dropIfExists('employees_view');
    }
}