Attempt to read property "name" on bool laravel 8, variable not transferring to view
You are encountering a classic type coercion issue in PHP that manifests differently depending on the runtime context. The error message Attempt to read property "name" on bool is not a mystery bug within Laravel; it is a direct consequence of how the route parameter $id is being resolved and passed between your controller and your view.
In your web.php route definition, you are defining the segment as {id}:
Route::get('employees/{id}/payslip', ['App\Http\Controllers\PrintController', 'print'])->name('employees.payslip');
When Laravel processes this request, it attempts to resolve the Employees model using that $id. However, the critical failure happens inside your print method. You are calling Employees::findOrFail($id). The find method returns an instance of the model if found, but returns false (a boolean) if no record matches the given identifier.
The root cause is almost certainly that $id is not a valid integer or string matching your database primary key. Instead, it is likely being passed as a boolean true or false. When you use dd($employees) in development mode, PHP often formats booleans in a way that makes debugging tricky if you aren't explicitly checking the type, or the dump might show the raw value which you missed. However, once you attempt to access $employee->name, PHP throws a fatal error because you cannot read an object property on a boolean value.
This usually happens when the route parameter is sanitized incorrectly before hitting the controller, or if there is a mismatch in how the ID is generated versus how it is expected by the Eloquent query. For instance, if your Employees table uses a UUID as the primary key and your frontend sends a numeric ID like 12, Laravel will return false. If your primary key is an auto-incrementing integer but you are passing true (perhaps due to a JavaScript type coercion issue where null becomes undefined or a specific logic error in the route), find returns false.
To fix this, you must ensure that the value passed into the controller method is treated as a string or integer before querying the database. You should validate and cast the $id parameter at the very beginning of your print function. This prevents the boolean from ever reaching the findOrFail call:
public function print($id)
{
// Cast $id to an integer or string to ensure it matches the DB key type
$id = (int)$id;
// Alternatively, if your IDs are UUIDs, cast to string:
// $id = (string)$id;
// Now findOrfail will throw a proper ModelNotFoundException
// instead of returning false if nothing is found.
$employees = Employees::findOrFail($id);
$teams = Team::all();
return view('employees.payslip', compact('employees', 'teams'));
}
Additionally, look at your Blade template. You are using @forelse($employees as $employee). This implies you expect $employees to be a collection (an array of items). However, your controller returns $employees, which is a single model instance because findOrFail retrieves exactly one record.
When you pass a single model instance into a loop like @forelse, Blade treats it as an iterable list containing that single item. While this often works for simple properties, accessing nested relationships or specific attributes can sometimes behave unexpectedly depending on the Laravel version and how the model is serialized. However, the primary crash you are seeing (read property "name" on bool) confirms the issue lies in the controller returning false rather than a model instance.
If you ever encounter this error in production without verbose logging enabled, ensure your route parameters are strictly typed or cast early. In Laravel 8, strict typing isn't enforced on route arguments by default, so manual casting is required to handle edge cases where the frontend sends an unexpected value. This approach ensures that findOrFail always receives a valid key type, preventing the method from returning a boolean and crashing your view rendering process.
