0

我将 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>
4

1 回答 1

1

我认为 $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
于 2021-06-01T09:49:03.530 回答