drehimself / laravel-ecommerce-example

Code for YouTube series on building a Laravel E-Commerce application.
https://www.youtube.com/watch?v=o5PWIuDTgxg&list=PLEhEHUEU3x5oPTli631ZX9cxl6cU_sDaR
1.01k stars 585 forks source link

Class "App\Product" not found error being shown? #107

Open rossi99 opened 3 years ago

rossi99 commented 3 years ago

Hi there, I am following this tutorial on the latest version of Laravel. And when I am trying to dynamically render my shopping cart I get the following error:

Class "App\Product" not found (View: /Users/rosscurrie/mobile-mastery-latest/resources/views/cart/cart.blade.php)

This is my Laravel version: "laravel/framework": "^8.12"

The file directory changes in this version. The product model is now located at: app\Models\Product.php instead of app\Product.php and I think this is the cause of the error.

My CartController.php looks like this:

<?php

namespace App\Http\Controllers;

use App\Models\Product;

use Gloudemans\Shoppingcart\Facades\Cart;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contracts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Redirector;

class CartController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Application|Factory|View|Response
     */
    public function index()
    {
        return view('cart.cart');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param Request $request
     * @return Application|RedirectResponse|Redirector
     */
    public function store(Request $request)
    {
        Cart::add($request->id, $request->name, 1, $request->price)
            ->associate('App\Models\Product'); // this allows us to retrieve our model easier

        return redirect()->route('cart.index')->with('success_message', 'Item successfully added to cart!');
    }

My cart.blade.php looks like this:

@foreach(Cart::content() as $item)
    <div class="cart-item-spacer">
        <div class="cart-item-container glass-effect">
            <div class="cart-image-container">
                <img src="{{ asset( 'img/products/'. $item->model->slug .'.jpg' ) }}" alt="{{ $item->model->slug }}" class="cart-image">
            </div>

            <div class="cart-info-container">
                <div class="item-info-container">
                    <p class="item-title">{{ $item->model->name }}</p>
                    <p class="item-price">{{ $item->model->price }}</p>

                    <div class="quantity-container">
                        <select class="quantity" data-id="{{ $item->rowId }}" data-productQuantity="{{ $item->model->quantity }}">
                            @for ($i = 1; $i < 5 + 1 ; $i++)
                                <option {{ $item->qty == $i ? 'selected' : '' }}>{{ $i }}</option>
                            @endfor
                        </select>
                    </div>
                </div>

                <div class="remove-action">
                    {{-- <form action="{{ route('cart.destroy', $item->rowId) }}" method="POST">--}}
                    <form action="" method="POST">
                        @csrf
                        @method('DELETE')

                        <button type="submit" class="remove-btn">
                            <span class="item-remove fas fa-times"></span>
                        </button>
                    </form>
                </div>
             </div>

            <div class="clearFix"></div>
        </div>
    </div>
@endforeach

Why am I getting this error? I thought I had fixed it by changing ->associate('App\Product'); to this: ->associate('App\Models\Product');

vkn999 commented 3 years ago

The same problem.

blwsh commented 3 years ago

Is this something you've implemented yourself recently? You could try running composer dumpautoload?