Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
rossi99 opened this issue Mar 8, 2021 · 2 comments
Open

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

rossi99 opened this issue Mar 8, 2021 · 2 comments

Comments

@rossi99
Copy link

rossi99 commented Mar 8, 2021

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
Copy link

vkn999 commented Mar 9, 2021

The same problem.

@blwsh
Copy link

blwsh commented Mar 29, 2021

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants