Getting an error as : Property [prod_name] does not exist on this collection instance

Viewed 39

When I am creating add to cart page in Laravel, Got an error as,

Exception
Property [prod_name] does not exist in this collection instance. (View: C:\xampp\htdocs\bizzcomputer\resources\views\cart.blade.php)

This is my CartController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Product;
use App\Category;


class CartController extends Controller
{
    
    public function index()
    { 
         $products = Product::get();
         return view ('cart')->with(compact('products'));
    }

    
    public function create()
    {
        //
    }

    
    public function store(Request $request)
    {
        //
    }

    
    public function show($id)
    {
       $product = Product::find($id);
        return view('cart')->with(compact('product'));
   }

    
    public function edit($id)
    {
        //
    }

    
    public function update(Request $request, $id)
    {
        //
    }

  
    public function destroy($id)
    {
        //
    }
}

This is my product page href to the cart page. When clicking on Add to Cart, It directs to cart page.

 <div class="ml-md-3">
                                        <a href="{{route('cart',$product->id)}}" class="btn px-5 btn-primary-dark transition-3d-hover"><i class="ec ec-add-to-cart mr-2 font-size-20"></i> Add to Cart</a>
                                    </div>

This is my route.

web.php

Route::get('/cart/{id}', 'CartController@show')->name('cart');
Route::get('/cart', 'CartController@index')->name('cart');

What could be the issue? Anyone please guide me to find out this?

1 Answers

There are many products in the collection $products, you need to loop on it to get the prod_name

@foreach($products as $product)
    <td data-title="Product">
        <a href="#" class="text-gray-90">{{ $product->prod_name }}</a>
    </td>
@endforeach
Related