Home > database >  Error with trying to get non-object property in blade
Error with trying to get non-object property in blade

Time:10-12

So I am trying to pass data to view:

$orders = $this
        ->query()
        ->where('user_id', '=', $id)
        ->with(['payment', 'payment.paymentStatus'])
        ->get();

return view('profile.profile-orders', compact('orders'));

I get this payload:

{
  "id": 4,
  "order_id": 4,
  "status": 1,
  "price": 897,
  "payment_date": "2021-10-11 18:11:50",
  "payment_status": {
    "id": 1,
    "title": "Not paid",
    "status_color": "#d1dade",
  },
  "order": {
    "id": 4,
    "customer_id": 4,
    "created_at": "2021-10-11T18:11:33.000000Z",
    "updated_at": "2021-10-11T18:11:33.000000Z",
    "items": [
      {
        "id": 5,
        "amount": 1,
        "price": 697,
        "product": {
          "id": 1,
          "price": "697.00",
          "order": 1,
          "images": [
            {
              "img": "image1"
            },
            {
              "img": "image2"
            }
          ],
        }
      }
    ]
  }
}

And inside my view I try to get payment_status->title

@foreach($orders as $order)
<tr>
   <td>{{ $order->payment->payment_status->title }}</td>
</tr>
@endforeach

But I get this error Trying to get property 'title' of non-object How do I fix this this error?

CodePudding user response:

I think all you need is an array access ([]) instead of an object access (->). so your code should be:

@foreach($orders as $order)
<tr>
   <td>{{ $order->payment->payment_status['title'] }}</td>
</tr>
@endforeach

CodePudding user response:

You can try something like this.

@foreach($orders as $order)
    <tr>
        <td>{{ $order->payment_status->title }}</td>
    </tr>
@endforeach
  • Related