@noblemfd what line is your error on?
Nov 30, 2020
12
Level 9
ERROR: Call to undefined method Barryvdh\DomPDF\PDF::with()
In my Laravel-5.8, I am trying to use Barryvdh DomPDF to export to PDF.
class AppraisalGoal extends Model
{
protected $table = 'appraisal_goals';
protected $fillable = [
'id',
'goal_type_id'
'appraisal_identity_id',
'employee_id',
'is_mid_year_published',
'is_visible',
'is_special_project',
];
public function goaltype()
{
return $this->belongsTo('App\Models\Appraisal\AppraisalGoalType','goal_type_id');
}
public function appraisalidentity()
{
return $this->belongsTo('App\Models\Appraisal\AppraisalIdentity','appraisal_identity_id');
}
public function appraisalgoaldetail(){
return $this->hasMany('App\Models\Appraisal\AppraisalGoalDetail');
}
public function company()
{
return $this->belongsTo('App\Models\Organization\OrgCompany','company_id');
}
}
Here is the controller:
public function appraisalPdf(Request $request)
{
$userCompany = Auth::user()->company_id;
$userEmployee = Auth::user()->employee_id;
$employee = HrEmployee::where('id', $userEmployee)->first();
$company = OrgCompany::where('id', $userCompany)->first();
$render=[];
$currentappraisal = AppraisalIdentity::select('appraisal_name', 'appraisal_start', 'appraisal_end')->where('company_id', $userCompany)->where('is_current', 1)->first();
$finalratingstatus = AppraisalParameter::select('activate_final_rating')->where('company_id', $userCompany)->first();
$currentappraisalstatus = AppraisalIdentity::select('is_current')->where('company_id', $userCompany)->where('is_current', 1)->first();
$goals = AppraisalGoal::where('employee_id', $userEmployee)->where(function ($query) {
$query->where('is_approved', 3)->orWhere('is_visible', 0);
})->whereNull('deleted_at')->orderBy('goal_type_id');
$goals=$goals->with('appraisalidentity');
if(isset($request->appraisal_identity_id))
{
$goals=$goals->where('appraisal_identity_id',$request->appraisal_identity_id);
$render['appraisal_identity_id']=$request->appraisal_identity_id;
}
$goals= $goals->get();
$data['goals'] = $goals;
$data['appraisalidentities']= AppraisalIdentity::where('company_id', $userCompany)->pluck('appraisal_name','id');
$pdf = PDF::loadView('appraisal-default-pdf', $data)
->with('employee', $employee)
->with('company', $company)
->with('finalratingstatus', $finalratingstatus)
->with('currentappraisalstatus', $currentappraisalstatus)
->with('currentappraisal', $currentappraisal);
return $pdf->setPaper('a4', 'landscape')->download('AppraisalReport.pdf');
}
route:
Route::get('/appraisal-default-pdf', 'HomeController@appraisalPdf')->name('appraisal-default-pdf');
Route::get('/appraisal-default', 'HomeController@appraisal')->name('appraisal-default');
View blade: appraisal-default
<section class="content">
<div class="container-fluid">
{{ Form::model(request(),['method'=>'get']) }}
<div class="row" style="margin-bottom: 10px">
<div class="col-sm-11">
{{ Form::select('appraisal_identity_id',$appraisalidentities,null,['class'=>'form-control select2bs4','placeholder'=>'Select Appraisal Period']) }}
</div>
<div class="col-xs-2">
{{ Form::submit('Search',['class'=>'btn btn-warning']) }}
</div>
</div>
{{ Form::close() }}
</div>
</section>
@foreach($goals as $key => $goal)
<td>
{{$goal->goaltype->name ?? '' }}
</td>
<td>
<b>{{$goal->goal_title ?? '' }}</b>
</td>
@endforeach
<div class="card-footer">
<a href="{{ route('appraisal-default-pdf') }}" target="_blank" class="btn btn-success" style="margin-right: 5px;">
<i class="fa fa-print"></i> Print
</a>
</div>
I filter the table in the view blade with a dropdown using: $appraisalidentities. This works fine.
How when I tried to export to PDF using {{ route('appraisal-default-pdf') }}, I got this error:
ERROR: Call to undefined method Barryvdh\DomPDF\PDF::with()
How do I resolve this?
Thanks
Level 54
@noblemfd is $goals in your controller method anymore?
according to your snippet it was:
$goals= $goals->get();
$data['goals'] = $goals;
$data['appraisalidentities']= AppraisalIdentity::where('company_id', $userCompany)->pluck('appraisal_name','id');
you don't need to define a base $data array, just add 'goals' and 'appraisalidentities'
$pdf = PDF::loadView('appraisal-default-pdf', compact(
'appraisalidentities',
'goals',
'employee',
'company',
'finalratingstatus',
'currentappraisalstatus',
'currentappraisal'
));
Please or to participate in this conversation.