TortleTurtle
TortleTurtle

Reputation: 23

Blade - Trying to get property of non-object

I have a view which displays details about an order but is unable to read the property name of owner.

Error Message

Trying to get property 'name' of non-object (View: C:\xampp\htdocs\Cuppy_Webshop\resources\views\orders\showOrder.blade.php)

Controller

public function show($id){
        $order = Order::with(['owner' => function ($query){
            $query->select('id', 'name');
        }])->where('id', '=', $id)->firstOrFail();

        return view('orders/showOrder', [
            'order' => $order,
        ]);
    }

Model

public function owner(){
        return $this->belongsTo('App\User', 'owner');
    }

Blade

<tbody>
                    <tr>
                        <td scope="col">{{$order->id}}</td>
                        <td scope="col">{{$order->clip}}</td>
                        <td scope="col">{{$order->engraving}}</td>
                        @if ($order->engraving == 1)
                        <td scope="col">{{$order->front_img}}</td>
                        <td scope="col">{{$order->back_img}}</td>
                        @endif
                        <td scope="col">{{$order->location}}</td>
                        <td scope="col">{{$order->ordered_at}}</td>
                        <td scope="col">{{$order->delivered_at}}</td>
                        <td scope="col">{{$order->status}}</td>
                        <td scope="col">{{$order->cup_id}}</td>
                        <td scope="col">{{$order->owner->name}}</td>
                    </tr>
                </tbody>

JSON

I had the controller return json to see if something was wrong with the formatting or if the property name wasn't being send. Everything seems to be fine:

{
    "id": 1,
    "clip": 1,
    "engraving": 1,
    "front_img": "Zelda",
    "back_img": "Ultra Smurf",
    "location": "Foodlab",
    "ordered_at": "2019-11-28 15:30:37",
    "delivered_at": null,
    "status": "not payed",
    "cup_id": 1,

    //the owner object
    "owner": {
        "id": 1,
        "name": "Daan"
    }
}

What I tried

I tried calling the name of owner as a associative array but that returned null. I dumped $order in the view which did show owner having the property name. Googling just gave me results that came down to syntax errors or wrong use of ORM. Had a friend look over my code for syntax errors but found nothing.

Upvotes: 1

Views: 3523

Answers (2)

andrethedev
andrethedev

Reputation: 31

Maybe your model should be this way?

public function owner() {
   return $this->belongsTo(User::class, 'id_user');
}

Upvotes: 0

Rouhollah Mazarei
Rouhollah Mazarei

Reputation: 4153

It's because probably your record does not have owner. You can use whereHas instead of with in your query, or change this line:

<td scope="col">{{$order->owner->name}}</td>

to this:

<td scope="col">{{$order->owner ? $order->owner->name:"no owner"}}</td>

You can read more in laravel documentation.

Upvotes: 1

Related Questions