【问题标题】:Barryvdh/laravel-domPDF returning Attempt to read property "name" on boolBarryvdh/laravel-domPDF 返回尝试读取布尔属性“名称”
【发布时间】:2021-08-19 12:17:31
【问题描述】:

我使用 Laravel 8 和 PHP 8 以及 barryvdh/laravel-domPDF。我有一个显示员工信息的crud,你可以打印出他们的工资单。当我不通过 $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>

【问题讨论】:

  • 您尝试过什么解决问题的方法?主人,这甚至与dompdf 有关吗?这看起来像一个纯粹的模板问题

标签: php laravel dompdf barryvdh


【解决方案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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-25
    • 2021-08-08
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 2021-12-07
    • 2014-12-24
    相关资源
    最近更新 更多