3

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
1
  • 3
    $employees is a single model, not a collection of Employees. You're currently trying to loop through the properties of a single Employee. Remove the loop and it should work Commented Jun 4, 2021 at 12:23

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.