首页 > 解决方案 > Barryvdh/laravel-domPDF 返回尝试在 bool 上读取属性“名称”

问题描述

我将 Laravel 8 与 PHP 8 以及 barryvdh/laravel-domPDF 一起使用。我有一个显示员工信息的杂物,您可以打印出他们的工资单。当我不通过 $id 时,它可以正常工作并打印所有员工信息,但是当我这样做时,它会返回错误:

尝试在 bool 上读取属性“名称”

这是我的路线:

Route::get('/print/{id}', ['App\Http\Livewire\EmployeesTable', 'print'])->name('livewire.employees-table.print');

这是我的模型功能

  public function print($id){
  $employees = Employees::find($id);
  $teams= Team::all();
  $pdf = PDF::loadView('employees.payslip', compact('teams', 'employees'));
  return $pdf->download('invoice.pdf');
}

这是我的用户关系:

  public function employees(){
  return $this->hasMany(Employees::class);
}

这是我的员工关系:

public function user(){
  return $this->belongsTo(User::class);
}

.

<thead>
 @foreach ($employees as $employee)
 <tr>
   <td>
   {{$employee->name}} {{$employee->surname}}
   {{$employee->phone}}
   {{$employee->role}}
 </td>
 </tr>
 @endforeach
</thead>
<thead>
 @foreach ($teams as $team)
 <tr>
   <td>
   {{$team->companyName}}
   {{$team->street}}
   {{$team->compound}}
   {$team->suburb}}
   {$team->phone}}
 </td>
 </tr>
 @endforeach
</thead>
</table>

标签: phplaraveldompdfbarryvdh

解决方案


我认为 $employees返回空结果,所以在循环之前更好地检查

@if(isset($employees)&&count((array)$employees))
 @foreach ($employees as $employee)
 <tr>
   <td>
   {{$employee->name}} {{$employee->surname}}
   {{$employee->phone}}
   {{$employee->role}}
 </td>
 </tr>
 @endforeach
@endif

推荐阅读