代码:
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');
}
}
答案 0 :(得分:0)
您没有加入presidents
表。也包括在内。
$reports = DB::table('president_report')
->join('reports', 'reports.id', '=', 'president_report.report_id')
->join('quarter_report', 'president_report.report_id', '=', 'quarter_report.report_id')
->join('presidents','president_report.president_id','=','presidents.id')
->select('president_report.*','reports.*','quarter_report.*','presidents.id','presidents. P_name')
->latest('reports.created_at')
->get();
答案 1 :(得分:0)
您想将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();
答案 2 :(得分:0)
如果要使用laravel雄辩地使用如下:
在Report.php
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');
}
}
在Quarter.php
class Quarter extends Model
{
public function reports() {
return $this->belongsToMany('App\Report', 'quarter_report', 'quarter_id', 'report_id');
}
}
在President.php中
class President extends Model
{
public function reports() {
return $this->belongsToMany('App\Report', 'president_report', 'president_id', 'report_id');
}
}
在控制器中
function viewPDF()
{
$reports = Report::with(['quarters', 'presidents']);
if ( request('president') ) {
$reports = $reports->whereHas('presidents' , function ($query) {
$query->whereId( request('president') );
});
}
if ( request('quarter') ) {
$reports = $reports->whereHas('quarters' , function ($query) {
$query->whereId( request('quarter') );
});
}
$reports = $reports->latest()
->get();
$pdf = PDF::loadView('reports.test1', ['reports' => $reports]);
return $pdf->stream('reports.pdf');
}