代码
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)
我想用口才来做。拜托,我该怎么办?
1-表格 quarters
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-表格 presidents
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-表格 reports
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();
});
答案 0 :(得分:0)
这是this version of question的答案。
尝试使用别名加入表,如下所示
->join('president_report as pr', 'presidents.id', '=', 'pr.president_id')
和/或改为'pr.president_id'
使用DB::raw( 'pr.president_id' )
答案 1 :(得分:0)
使用DB :: raw()
function viewPDF()
{
$reports = Report::join('president_report','president_report.report_id' 'reports.id', '=', 'president_report.report_id')
->join('president_report as pr', DB::raw('presidents.id'), '=',DB::raw( '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');
}
答案 2 :(得分:0)
我认为您已与模型
建立了联系(1)第一个选项
CONTROLLER
在您的控制器中,您可以这样做。
President::with('president_report')->get()->map(function($president){
return [
'president_id' => $president->id,
'president_report_id' => $president->president_report->id,
]
})
(2)第二选项
如果要访问父级父级关系。
quarters
属于president_report
,而president_reports
属于president
您可以这样做。
总裁模式
public function quarters() {
return $this->hasManyThrough(Quarter::class, President_report::class);
}
CONTROLLER
您可以在控制器中执行此操作。
$presidents = President::all();
$quaters = president->quarters()->where('quarters.report_id', 1)->get();
dd($quarters);