mysql - 如何使用 laravel eloquent 连接多个表?

标签 mysql laravel

代码

   function viewPDF()
{
    $reports = Report::join('president_report', 'reports.id', '=', 'president_report.report_id')
        ->join('president_report as pr', 'presidents.id', '=', 'pr.president_id')
        ->join('quarter_report', 'president_report.report_id', '=', 'quarter_report.report_id')
        ->filter()->latest()->get();
    $pdf = PDF::loadView('reports.test1', ['reports' => $reports]);
    return $pdf->stream('reports.pdf');
}

错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'presidents.id' in 'on clause' (SQL: select * from `reports` inner join `president_report` on `reports`.`id` = `president_report`.`report_id` inner join `president_report` as `pr` on `presidents`.`id` = `pr`.`president_id` inner join `quarter_report` on `president_report`.`report_id` = `quarter_report`.`report_id` order by `created_at` desc)

我想用 Eloquent 方式来做。请问,我该怎么办?

1-Table 季度

Schema::create('quarters', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->timestamps();
    });
    Schema::create('quarter_report', function (Blueprint $table) {
        $table->integer('quarter_id')->unsigned();
        $table->integer('report_id')->unsigned();
        $table->primary(['quarter_id' , 'report_id']);
    });

2- 表 总统

Schema::create('presidents', function (Blueprint $table) {
        $table->Increments('id');
        $table->string('P_name');
        $table->timestamps();
    });
 Schema::create('president_report', function (Blueprint $table) {
        $table->integer('president_id')->unsigned();
        $table->integer('report_id')->unsigned();
        $table->primary(['president_id','report_id']);
    });

3-表 报告

Schema::create('reports', function (Blueprint $table) {
        $table->Increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('target_value');
        $table->text('major_activity');
        $table->string('indicator');
        $table->string('responsibility');
        $table->string('progress_activity');
        $table->string('documents')->nullable();
        $table->string('percentage_activity')->nullable();
        $table->text('problem')->nullable();
        $table->text('solution')->nullable();
        $table->text('description')->nullable();
        $table->string('image')->default('default.png');
        $table->tinyInteger('status')->default('0');
        $table->tinyInteger('active')->default('0');
        $table->timestamps();
    });

I have 5 tables

最佳答案

这是 this version of question 的答案.

尝试如下别名连接表

->join('president_report as pr', 'presidents.id', '=', 'pr.president_id')

和/或 'pr.president_id' 使用 DB::raw( 'pr.president_id' )

关于mysql - 如何使用 laravel eloquent 连接多个表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58091633/

相关文章:

java - 调用等待数据库值的 Java 方法?

Laravel5.3 - 创建一定数量的相关模型 - 创建模型时

php - 我怎样才能传递一个变量

php - 获取id值 PHP、mysql、jquery

php - Laravel 5.2 队列和作业 - 不推送到作业数据库

php - Laravel 5 - 多语言网站(LTR 和 RTL)

html - 内容和页脚之间的空间

php - 拉拉维尔 : Image not uploaded

mysql - 在MySql中添加索引到日期字符串字段

java - 如何获得所有答案值?