Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adhik13th's avatar

Dompdf Error with Trying to get property 'colomn' of non-object

I have a view like that and I want to make a PDF download. My view doesn't have an error

    <div class="col-lg-12 col-xs-12">
        <div class="box">
            <a href="{{ route('pdf',['download'=>'pdf']) }}">Download PDF</a>//function to download
          <div class="box-header">
            <center > <h3 class="box-title">PEMELIHARAAN DAN PERAWATAN ALAT UJI </h3> </center>
            <center> Jln. Kabupaten Sragen </center>
          </div>
          <!-- /.box-header -->

             <p> Laporan : {{ $pemeliharaan->status }} </p>
             <p> Tanggal : {{ $pemeliharaan->created_at }} </p>
             <p> Jenis Alat   : {{ $pemeliharaan->alat->nama_alat }} </p>
             <p> User    : {{ $pemeliharaan->user->name }} </p>

             <div class="box">
                <div class="box-header">
                  <h3 class="box-title"></h3>
                </div>
                <!-- /.box-header -->
                <div class="box-body no-padding">
                  <table class="table table-condensed">
                    <tbody><tr>
                      <th style="width: 10px">#</th>
                      <th>Pertanyaan</th>
                      <th>Hasil</th>
                      {{--  <th style="width: 40px">Label</th>  --}}
                    </tr>
                    <tr>
                      <td>1.</td>
                      <td>{{ $pemeliharaan->pertanyaan['question1'] }}</td>
                      <td>
                          {{ $pemeliharaan->pertanyaan['answer1'] }}
                      </td>
                    </tr>

                    <tr>
                      <td>2.</td>
                      <td>{{ $pemeliharaan->pertanyaan['question2'] }}</td>
                      <td>
                          {{ $pemeliharaan->pertanyaan['answer2'] }}
                      </td>
                    </tr>

                    <tr>
                      <td>3.</td>
                      <td>{{ $pemeliharaan->pertanyaan['question3'] }}</td>
                      <td>
                          {{ $pemeliharaan->pertanyaan['answer3'] }}
                      </td>
                    </tr>

                    <tr>
                      <td>4.</td>
                      <td>{{ $pemeliharaan->pertanyaan['question4'] }}</td>

                        <td>
                          {{ $pemeliharaan->pertanyaan['answer4'] }}
                        </td>

                    </tr>
                  </tbody></table>
                </div>
                <!-- /.box-body -->
              </div>

          <!-- /.box-body -->
        </div>
        <!-- /.box -->
      </div>

    </div>

But this link 'download' has an error:

Trying to get property 'pertanyaan' of non-object

I don't know, but my view doesn't have an error.

This is my controller:

public function showQuestion(Request $request,$id)
    {
         $pemeliharaan = Pemeliharaan::find($id);

         $pemeliharaan->pertanyaan = json_decode($pemeliharaan->pertanyaan,true);

         if($request->has('download')){
            $pdf = PDF::loadView('users.view_question', $pemeliharaan);
            return $pdf->download('view_question.pdf');
        }

         return view('users.view_question',compact('pemeliharaan'));
    }

And this is my route to show view and get PDF:

Route::get('/user/show/question/pdf/{id}','userController@showQuestion')->name('pdf');
Route::get('user/show/question/{id}', 'userController@showQuestion')->name('usershowQuestion');

Can someone correct my code for the download?

0 likes
5 replies
bobbybouwmann's avatar
Level 88

The problem is not in your code but in the database. You do the following thing

$pemeliharaan = Pemeliharaan::find($id);

// This line is throwing the error
$pemeliharaan->pertanyaan = json_decode($pemeliharaan->pertanyaan,true);

The reason this is throwing an error because Pemeliharaan::find($id); returns null. So there is no object in the database with the given id. You need to make sure the id exists in the database.

You can throw an error whenever this happens within Laravel like so

$pemeliharaan = Pemeliharaan::findOrFail($id);

If the $id does not exists in the database you will see a 404 page

bobbybouwmann's avatar

@adhik13th Well that is the problem. There is no id given to the url so you can't find the Pemeliharaan model in the database.

So you need to make sure the variable is filled.

In your case the url should look like this: my url : http://localhost:8000/user/show/question/pdf/123

Replace 123 with the id in the database of that model.

You now get a 404 because the model doesn't exists!

Please or to participate in this conversation.