Reputation: 17
I have an error on my code of Productdetails View
Trying to get property 'name' of non-object
web.php file:
Route::delete('/Destroy/{id}','ProductsController@destroy');
Route::get('/Edit/{id}', 'ProductsController@edit');
Route::post('/Update/{id}','ProductsController@update');
Route::get('/listing',function(){
$products = Product::all();
return view('pages.productlisting',compact('products'));
});
Route::get('Product-details/{id}','ProductsController@ProductDetail');
ProductDetail Function On controller
public function ProductDetail($id)
{
$products = Product::findOrFail($id);
return View('pages.productdetails', compact('products',$products));
}
view file:
@foreach($products as $product)
<h1>{{$product->name}}</h1>
@endforeach
And I have a problem in my view in data attributes
Mydatabase attributes
name, price, category, description,
platform.
I need to pass these data on product details view
Upvotes: 1
Views: 40
Reputation: 15316
With this, you're getting a single collection.
$products = Product::findOrFail($id);
You don't need to pass it in foreach loop
. Change in your view file as below. Remove foreach.
<h1>{{$products->name}}</h1>
If you use get()
method you would get multiple objects where you can use foreach
.
But for find()
and first()
methods return single object so no need for looping.
Upvotes: 1