Skip to content
Advertisement

Class ‘AppmodelProduct’ not found in category.blade.php

For my Laravel assignment I am working on a store, and using models to create the pages of shown products. However, the view pages gives me the error of class not found. I am a bit stumped on that to be honest. Here is the controller I use:

namespace AppHttpControllersShop;
use AppHttpControllersController;
use IlluminateHttpRequest;
use AppModelsCategory;
use AppModelsProduct;

class ShopController extends Controller{
    
    public function displayProduct($cat, $pro) {
    $data['category'] = Product::getProduct($cat, $pro);
    return view('shop.prosuct', $data);
    }
    
    public function displayCategory($slug) {
        $data['category'] = Category::getCategory($slug);
        return view('shop.category', $data);
    }
    public function displayShop() {
        $data['categories'] = Category::getCategories();
        return view('shop.shop', $data);
    }
}
    

Said Model:

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Product extends Model {
    public function category() {
        return $this->belongsTo('App/Model/Category');
    }
    
    public static function getProduct($cat, $pro){
        $product = self::where('slug', $pro)->firstorFail();
        $product_cat = $product->category->slug;        
        //retun ($product_cat === $cat) ? $product_cat: false;
        abort_if($product_cat !== $cat, 404);
        return $product;
    }
    
    
    //use HasFactory;
}

the Category model (just in case):

<?php

namespace AppModels;
//use AppHttpModles;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;

class Category extends Model
{
    public function products() {
        return $this->hasMany('AppModelProduct');
    }  
    
    public static function getCategory($slug){
        return self::where('slug', $slug)->firstOrFail(['id', 'slug']);      
    }    
    public static function getCategories() {
        return self::orderBy('slug')->get();        
    }
    //use HasFactory;
}

and finally, the view page:

@extends('template')
@section('content')

<h1 class="mb-5"> {{$category->name}} </h1>
<div class="row">
    @foreach ($category->products as $product)
    <div class="col-md-4 mb-5">
        <div class="pro-container">
            <h3>{{$product->name}}</h3>
            <a href="{{url('shop/' . $category->slug . '/' .$product->slug)}}"><img src="{{asset('images/products/' . $product->image)}}"></a>
            <h4> ₪ {{$product->price}} </h4>
            <a class="btn btn-primary" herf=""> ADD TO CART </a>
            <a class="btn btn-primary" herf="{{url()->current() . '/' . $product->slug}}"> READ MORE </a>
        </div>
    </div>
    @endforeach
</div>

@endsection

The idea is to save the space and create the page of each category to show its products; I have 3 categories in total it needs to be done for.

Thank you in advance for your help! (also, I’m using Laravel 7)

Advertisement

Answer

In both of your relationships you are referencing the Model directory instead of the Models one:

# Category.php

public function products() {
    return $this->hasMany('AppModelProduct');
}                              ^^^^^^

# Product.php

public function category() {
    return $this->belongsTo('AppModelCategory');
}                               ^^^^^^^^

Fix them and it should work now.

To avoid this kind of issues, given that you’re dealing with strings, why not make use of this other (better) syntax? It’s also IDE-friendly and supports refactoring:

# Category.php

public function products() {
    return $this->hasMany(Product::class);
}                         ^^^^^^^^^^^^^^

# Product.php

public function category() {
    return $this->belongsTo(Category::class);
}                           ^^^^^^^^^^^^^^^
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement