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
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
heredd($employee);
will returntrue
so it throwing erorr
Also keep in mind if no record then it might return null so make sure to chek likeisset
or$employees->name??null
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Laravel is a free open source PHP framework that came out in 2011. Since then, it has been able to become the framework of choice for web developers. One of the main reasons for this is that Laravel makes it easier, faster, and safer to develop complex web applications than any other framework.
https://laravel.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.