0

Since I started with my first Laravel project I've been having the error Trying to get property 'column_name' of non-object nonstop, and most of the time I've been able to fix it one way or another, but the problem is, I have no idea of why I get the error, or how can I prevent it. For example,

// In my controller I have this domPDF function...
public function pdfgen(Request $request, $id) {
        $data = Table::find($id);
        View()->share('data', $data);

        if ($request -> has('download')) {
            $pdf = PDF::loadView('pdf/pdfgen');
            return $pdf ->download('pdfgen.pdf');
        }

        return view('pdf/pdfgen');
}
// In my blade file I have a table calling this column...
<td>{{ $data->column }}</td>

// and this link making the request in the pdfgen function... 
<a class="btn btn-primary" href="{{ route('pdfgen', ['download'=>'pdf']) }}">Download PDF</a>
// And in my routes of web.php...
Route::get('/pdfgen/{id}', array('as' => 'pdfgen', 'uses' => 'PDFController@pdfgen'));

and whenever I press that download link, I get a Whoops page with the trying to get property 'column' of non-object (View: /usr/src/workapp/resources/views/pdf/pdfgen.blade.php). Why am I getting this error and not the open/save dialog?

EDIT: Forgot to add, before trying to get specific data with $id, I called the whole table ($data = Table::all();) and used a @foreach in the blade view to print it in the table

EDIT 2: Adding the model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Table extends Model
{
    protected $table = 'table';
    protected $primaryKey = 'id';
    public $incrementing = false;

    protected $fillable = [
        // all columns are strings, even id
        'id',
        'column',
        'column2',
        'column3',
    ];

    public function table2()
    {
        return $this->hasMany('App\Table2');
    }
}
  • 1
    An attempt to address something that IS NOT an object as if it were an Object. Normally because something a little earlier in the code returned an error (normally a boolean) rather than the object it was expected to return. Which means you didnt check for errors – RiggsFolly Feb 14 '19 at 14:15
  • You'll have to check the documentation to see how to pass the `$data` into `PDF::loadView`. I'm guessing it doesn't get the share from `View::share` – aynber Feb 14 '19 at 14:16
  • Are you sure the `$id` exists in the database? – Jerodev Feb 14 '19 at 14:17
  • @Jerodev I made sure I used an `$id` that exists in the table (1234 because it's easy to remember) – AEinarr Krigsson Feb 14 '19 at 14:31
  • Also I forgot to add that it worked when it didn't have the `$id`, calling all the table and using a `@foreach` to access the data – AEinarr Krigsson Feb 14 '19 at 14:33
  • @RiggsFolly but how do I know when I'm not passing an object? (besides the error) also to be honest I have no idea how to check for errors – AEinarr Krigsson Feb 14 '19 at 14:54
  • You check that `$data` returned from this call `$data = Table::find($id);` is not the Error value which I assume will be `false` – RiggsFolly Feb 14 '19 at 14:57
  • @RiggsFolly Right now the view itself does return the data requested, but when I press the "download" button the error page appears – AEinarr Krigsson Feb 14 '19 at 15:13
  • What is the exact error message? Namely, which file is causing the error? There are some instances where the document fed to Dompdf can cause this problem (see https://github.com/dompdf/dompdf/issues/1406) because of how Dompdf is parsing the document. – BrianS Feb 16 '19 at 21:41
  • I think the problem was with the laravel wrapper I was using (barryvdh/laravel-dompdf) because as soon as I uninstalled that one and installed the normal dompdf the problem went away. (sadly now I have to use html inside a $html variable, but it's better than not being able to go forward) Also I forgot to say I was using a wrapper – AEinarr Krigsson Feb 18 '19 at 14:54

2 Answers2

2

When you do find($id) and the record is not present, it returns null. So if you try to access any property ->column, it will throw error.

You can either do findOrFail($id) or have condition if (is_null($data)) or on frontend have $data->column ?? 'defaultvalue '

Now, the second situation where your model is present but you still get error, do :

$data = Table::find($id);
dd(array_keys($data->toArray()));

You should see the column you are trying to access if it is present.

Lastly, if it is not present in above step but is there in the table, then check for protected $hidden = [] setting of your model which essentially hides certain columns.

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37
  • While trying to fix that problem, I used `$data = Table2::with('Table')->where('id', $id)->get();` and for some reason it works with no problem, but if I use `->first();` instead of `->get();` it returns the `Trying to get property 'column' of non-object` error, why is that? – AEinarr Krigsson Feb 15 '19 at 13:18
  • Do you have any accessor or an appended property on that model? – Mihir Bhende Feb 15 '19 at 13:19
  • what is an accessor and an appended model? – AEinarr Krigsson Feb 18 '19 at 14:54
  • [accessor](https://laravel.com/docs/5.7/eloquent-mutators#defining-an-accessor) and [appended properties](https://laravel.com/docs/5.7/eloquent-serialization#appending-values-to-json) :) In short appended property is an additionlal property which is not one of your columns. Like full_name which combo of first and last name. Accessor is a function which modifies the bsic vaue of a column. – Mihir Bhende Feb 18 '19 at 14:57
  • Thanks c: I think the problem was with the dompdf wrapper I was using (barryvdh/laravel-dompdf, which I forgot to add in the question) because when I uninstalled it and installed the normal version of dompdf the problem stopped appearing (with that particular controller) – AEinarr Krigsson Feb 18 '19 at 15:25
0

This happend because your $data might be null and your record with given $id might not exists in your database.

Try to change your code like below:

Table::findOrFail($id);

From now on if your table with given id doesn't exist, it'll automatically return 404 page.

Soheil Rahmat
  • 491
  • 3
  • 15