Laravel 5.2 中的 Mysql View

标签 mysql laravel-5 sql-view

这对某些人来说可能很简单。但对我来说,我完全迷路了。谁能提醒我在 Laravel 5 中使用 Mysql View。我已经搜索了一段时间的相关帖子,但没有任何线索,除了:

DB::statement("Create View")

DB::statement("Drop View")

但这并没有敲响警钟。感谢任何帮助,任何线索,任何指南。 提前致谢

我的场景

I have an employee table with other tables that holds various attributes of the employee separately such as Appointment, posting,health, family etc etc. Most of these tables has one property Is_current to represent the current record of the employee. So whenever I want to display employee profile with latest record or retrieve some latest record from some of these tables, I don't want to retrieve from each an every table one by one. I just want to compile the latest record in a view and retrieve from it whenever I want.

希望您能理解我的要求,抱歉我的英语不好

最佳答案

我一直将 View 用于报告目的,因为我可以创建一个非规范化 View ,然后使用具有范围和修改器的模型的强大功能。我写了一篇关于如何管理 MySQL View 的文章。

# Create a new migration
php artisan make:migration create_employees_record_view

# Update the migration
<?php

use Illuminate\Database\Migrations\Migration;

class CreateEmployeesRecordView extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        \DB::statement("
            CREATE VIEW employees_records 
            AS
            SELECT
                employees.emp_no,
                employees.first_name,
                employees.last_name,
                employees.gender,
                employees.hire_date,
                employees.birth_date,
                dept_emp.dept_no,
                departments.dept_name,
                mananger.emp_no AS manager_emp_no,
                mananger.first_name AS manager_first_name,
                mananger.last_name AS manager_last_name
            FROM
                employees
                LEFT JOIN dept_emp ON employees.emp_no = dept_emp.emp_no
                LEFT JOIN departments ON dept_emp.dept_no = departments.dept_no
                LEFT JOIN dept_manager ON departments.dept_no = dept_manager.dept_no
                LEFT JOIN employees mananger ON dept_manager.emp_no = mananger.emp_no;
        ");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
    }
}

# Run the migration
php artisan migrate

通过控制台命令管理它

php artisan make:command CreateOrReplaceEmployeeRecordsViewCommand

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class CreateOrReplaceEmployeeRecordsViewCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'view:CreateOrReplaceEmployeeRecordsView';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create or Replace SQL View.';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \DB::statement("
            CREATE OR REPLACE VIEW employees_records 
            AS
            SELECT
                employees.emp_no,
                employees.first_name,
                employees.last_name,
                employees.gender,
                employees.hire_date,
                employees.birth_date,
                dept_emp.dept_no,
                departments.dept_name,
                mananger.emp_no AS manager_emp_no,
                mananger.first_name AS manager_first_name,
                mananger.last_name AS manager_last_name
            FROM
                employees
                LEFT JOIN dept_emp ON employees.emp_no = dept_emp.emp_no
                LEFT JOIN departments ON dept_emp.dept_no = departments.dept_no
                LEFT JOIN dept_manager ON departments.dept_no = dept_manager.dept_no
                LEFT JOIN employees mananger ON dept_manager.emp_no = mananger.emp_no;
        ");
    }
}

使用模型查看 # 创建一个新模型 php artisan make:model 员工记录

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class EmployeesRecord extends Model
{
}

测试新创建的模型

# For this we will be using tinker
php artisan tinker

>>> $e = \App\EmployeesRecord::first();
=> App\EmployeesRecord {#2885
     emp_no: 10001,
     first_name: "Georgi",
     last_name: "Facello",
     gender: "M",
     hire_date: "1986-06-26",
     birth_date: "1953-09-02",
     dept_no: "d005",
     dept_name: "Development",
     manager_emp_no: 110511,
     manager_first_name: "DeForest",
     manager_last_name: "Hagimont",
   }
>>> $e = \App\EmployeesRecord::where('emp_no', 10003)->first();
=> App\EmployeesRecord {#2896
     emp_no: 10003,
     first_name: "Parto",
     last_name: "Bamford",
     gender: "M",
     hire_date: "1986-08-28",
     birth_date: "1959-12-03",
     dept_no: "d004",
     dept_name: "Production",
     manager_emp_no: 110303,
     manager_first_name: "Krassimir",
     manager_last_name: "Wegerle",
   }

引用 - http://blog.tekz.io/laravel-eloquent-how-to-effectively-manage-sql-views/

关于Laravel 5.2 中的 Mysql View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38767738/

相关文章:

mysql - 如何在比较 MySQL 中的两个表后拖动值

laravel-5 - 在 Laravel 5.5 中移动文件夹

php - 使用 Laravel(或不)合并 mysql 表中的记录?

mysql - 如何在 Sphinx 搜索中手动添加行到索引?

java - JSP for 循环与数据库结果

php - mysql查询和输出

php - Laravel 5.2:Auth::logout() 不工作

sql - MySQL View : Referencing one calculated field (by name) in another calculated field

sql - PostgreSQL 中的 TRIGGER ON VIEW 不会触发

sql - 通过触发器更新/插入 View