I'm trying to load a single blog post in laravel, but every time I click on a single post, I get the error 'Attempt to read property "title" on bool.' I've tried looking up the error but I haven't found a solution and don't know what causes the error. Here's my code
Blade file:
@foreach($AboutUs as $about_us)
<div class="col-lg">
<img src="images/agenda.png" class="img-fluid shadow-lg" style="">
<p class="mt-3 text-success fw-bold fs-5 text-lg-start">{{$about_us->title}}</p>
<p class="fs-6 text-lg-start"> {{$about_us->description}}</p>
<p class="text-success fw-bold text-lg-start"><a href="/about.show/{{$about_us->id}}" class="text-success text-decoration-none">{{$about_us->button}}</a></p>
<!--<img src="images/hr-2.png" class="img-fluid float-start my-2">-->
</div>
@endforeach
Controller:
public function show($id)
{
$data = AboutUs::find($id);
return view('about.show', ['AboutUs' => $data])
->with(['AboutUs' => AboutUs::find($id)]);
}
Routes:
Route::get('/about.show/{id}', 'App\Http\Controllers\WelcomeController@show')->name('about.show');
Can anyone offer any assistance?
OK, it's a bit of a mess, to be honest.
The short answer is that :
$data = AboutUs::find($id);
is returning one item, not a collection of items. You can't loop through that like you're trying to do, hence the error.
For some reason, though, you're then loading that again with when you're returning your view?
return view('about.show',['AboutUs'=>$data])->with(['AboutUs' => AboutUs::find($id),]);
So, for starters, you can probably simplify that - assuming you're wanting to load a post, in your controller you can then instead :
public function show($id) {
$post = AboutUs::find($id);
return view('about.show')->with(compact('post');
}
But then in your blade file you're trying to loop through your $AboutUs variable as though it's a collection, but it's not - find($id) returns a single model instance.
So naturally Blade is getting a bit confused.