Minh Quân
Minh Quân

Reputation: 11

Trying to get property 'product_id' of non-object

enter image description hereI was using the laravel shopping cart when I got this error. Please ask how to fixenter image description here

enter image description here

Upvotes: 0

Views: 324

Answers (1)

Sarvil Ajwaliya
Sarvil Ajwaliya

Reputation: 233

As per your code you are trying to access data directly without checking if data is return from the query:

You need to check first:

if ($product_info) {
 // do your code here
}

then redirect to your desire page

The product your are looking for is does not exist that's why you got that error.

Or else you can also use below query:

DB::table('tbl_product')->where('product_id', $productId)->firstOrFail();

The findOrFail methods will retrieve the first result of the query; however, if no result is found, a Illuminate\Database\Eloquent\ModelNotFoundException will be thrown:

Upvotes: 1

Related Questions