-1

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
1
  • Khotbah::find($id_khotbah); returns a single Khotbah Model instance, or null. foreach() on a single Model instance will loop it's public properties, like property => true, so $dataKhotbah on the first iteration is a boolean true (or false), and true->judul_khotbah is 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. Commented Oct 2, 2023 at 16:41

1 Answer 1

0

dont loop it, just delete your foreach, you are fetching one record.

            <div class="book">
                <div class="page">
                    <div class="subpage">
                        <h1>Judul Khotbah : {{ $khotbah->judul_khotbah }}</h1>
                        <h5>Tanggal : {{ $khotbah->tanggal_khotbah }}</h5>
                        <h5>Pembicara : {{ $khotbah->pembicara }}</h5>
                        <br>
                        <p>{{ $khotbah->isi_khotbah }}</p>
                    </div>
                </div>
            </div>

Attempt to read property "name" on bool laravel 8, variable not transfering to view

thats the references

and i suggest you to do ::findOrFail that will help you to abort the task if data is empty

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.