Reputation: 69
I keep encountering this error on Laravel 8 with PHP 8. Im grabbing the id from the view like so:
<button type="submit">
<a href="{{route('employees.payslip', $employee->id)}}" class="text-green-600 hover:text-green-900">Payslip</a>
</button>
This then goes to web.php like so:
Route::get('employees/{id}/payslip', ['App\Http\Controllers\PrintController', 'print'])->name('employees.payslip');
It then goes to the print function like so:
public function print($id)
{
$employees = Employees::findOrFail($id);
$teams = Team::all();
return view('employees.payslip',compact('employees', 'teams'));
}
When i remove the return view line and replace it with:
dd($employees);
It gives me the correct information but when i keep the line:
return view('employees.payslip',compact('employees', 'teams'));
and send it the view 'employees.payslip': it gives me the error: anyone has any ideas?
@forelse($employees as $employee)
<tr>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{$employee->name}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{$employee->surname}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{$employee->address}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{$employee->phone}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{$employee->role}}
</td>
<td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
{{$employee->absent}}
</td>
</tr>
@empty
<td>
No Information to Display
</td>
@endforelse
Upvotes: 3
Views: 49979
Reputation: 15319
Since you are fetching one record from Employee model.So it return single object.
$employees = Employees::findOrFail($id);
so as @aynber said , you dont need loop.Its just like below to access data
{{$employees->name}}
If you loop then error says
Attempt to read property “name” on bool laravel 8, variable not transfering to view
@forelse($employees as $employee)
@php
dd($employee);
@endphp
@empty
<td>
No Information to Display
</td>
@endforelse
here dd($employee);
will return true
so it throwing erorr
Also keep in mind if no record then it might return null so make sure to chek like isset
or $employees->name??null
Upvotes: 3