Im using Laravel 9.52.10 and PHP 8.0.28. i have CRUD that display preaching data can print it. that data only can printed per ID, not all of data preaching. but i have an error:
Attempt to read property "judul_khotbah" on bool
this my route
Route::get('{id_khotbah}/khotbahPdf', [KhotbahController::class,'generatePdf'])->name('cetakPdf');
my controller
public function generatePdf(Request $request,$id_khotbah)
{
$khotbah = Khotbah::find($id_khotbah);
$pdf = Pdf::loadView('dashboard.khotbahPdf' ,compact('khotbah'));
return $pdf->download('RingkasanKhotbah.pdf');
}
My blade
<body>
@method('put')
@csrf
@foreach ($khotbah as $dataKhotbah)
<div class="book">
<div class="page">
<div class="subpage">
<h1>Judul Khotbah : {{ $dataKhotbah->judul_khotbah}}</h1>
<h5>Tanggal : {{ $dataKhotbah->tanggal_khotbah}}</h5>
<h5>Pembicara : {{ $dataKhotbah->pembicara}}</h5>
<br>
<p>{{ $dataKhotbah->isi_khotbah}}</p>
</div>
</div>
</div>
@endforeach
</body>
and if add to my blade
@if (isset($khotbah) && count((array) $khotbah))
like this
<body>
@method('put')
@csrf
@if (isset($khotbah) && count((array) $khotbah))
@foreach ($khotbah as $dataKhotbah)
<div class="book">
<div class="page">
<div class="subpage">
<h1>Judul Khotbah : {{ $dataKhotbah->judul_khotbah ?? null }}</h1>
<h5>Tanggal : {{ $dataKhotbah->tanggal_khotbah ?? null }}</h5>
<h5>Pembicara : {{ $dataKhotbah->pembicara ?? null }}</h5>
<br>
<p>{{ $dataKhotbah->isi_khotbah ?? null }}</p>
</div>
</div>
</div>
@endforeach
@endif
</body>
it only print html without data
Khotbah::find($id_khotbah);returns a singleKhotbahModel instance, ornull.foreach()on a single Model instance will loop it's public properties, likeproperty => true, so$dataKhotbahon the first iteration is a booleantrue(orfalse), andtrue->judul_khotbahis not valid PHP. Please read Laravel's documentation and learn the difference between->find(),->first(),->get(), etc., and how to work with a single Model instance vs a Collection or Array of Model instances.