代码
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();
});
最佳答案
这是 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/