码:
function viewPDF()
{
$reports = Report::with([
'quarters' => function($query) {
// filters on quarters table
return $query->where('column', 'value');
},
'presidents' => function($query) {
// filters on presidents table
return $query->where('column', 'value');
}
])
->filter() // only for reports table
->latest()
->get();
$pdf = PDF::loadView('reports.test1', ['reports' => $reports]);
return $pdf->stream('reports.pdf');
}
我想用口才来做。拜托,我该怎么办?
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();
});
报告模型
class Report extends Model
{
public function quarters() {
return $this->belongsToMany('App\Quarter', 'quarter_report', 'report_id', 'quarter_id');
}
public function presidents() {
return $this->belongsToMany('App\President', 'president_report', 'report_id', 'president_id');
}
public function scopeFilter($query){
$president = request('president');
if ( isset($president)&& trim($president) != '' && $president != 'all'){
$query->whereHas('presidents' , function ($query )use ($president){
$query->whereId($president);
});
}
$quarter = request('quarter');
if ( isset($quarter)&& trim($quarter) != '' && $quarter != 'all'){
$query->whereHas('quarters' , function ($query )use ($quarter){
$query->whereId($quarter);
});
}
}
总统模型
class President extends Model
{
public function reports() {
return $this->belongsToMany('App\Report', 'president_report', 'president_id', 'report_id');
}
}
四分之一模型
class Quarter extends Model
{
public function reports() {
return $this->belongsToMany('App\Report', 'quarter_report', 'quarter_id', 'report_id');
}
}
您想将presidents
表与president_report
表连接。但是您president_report
两次加入表。正确的方法应该是
$reports = Report::join('president_report', 'reports.id', '=','president_report.report_id')
->join('presidents', 'presidents.id', '=', 'president_report.president_id')
->join('quarter_report', 'president_report.report_id', '=', 'quarter_report.report_id')
->latest('reports.created_at')
->get();
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句