maxxxabro
1 week ago
113
8
Laravel

Trying to get property 'products' of non-object (View: D:\laragon\www\htdocs\products\resources\views\pages\productcategory.blade.php)

Posted 1 week ago by maxxxabro

productcategory.blade.php

 <div class="site-main">
        <div class="sidebar ttm-sidebar-right ttm-bgcolor-white clearfix">
            <div class="container">
                <!-- row -->
                <div class="row">
                    <div class="col-lg-12 content-area">

                        <div class="products row">
                            @foreach($productcategory->products as $pro)
                                <div class="product col-md-4 col-sm-6 col-xs-12">
                                    <a href="{{ route('product.single', ['slug' => $pro->slug ]) }}">
                                        <div class="ttm-product-box">
                                            <!-- ttm-product-box-inner -->
                                            <div class="ttm-product-box-inner">
                                                <div class="ttm-product-image-box">
                                                    <img class="img-fluid" src="{{ $pro->featured }}" alt="">
                                                </div>
                                            </div><!-- ttm-product-box-inner end -->
                                            <div class="ttm-product-content">
                                                <a class="ttm-product-title" href="{{ route('product.single', ['slug' => $pro->slug ]) }}">
                                                    <h2>{{ $pro->title }}</h2>
                                                </a>
                                                <div class="star-ratings">
                                                    <ul class="rating">
                                                        <li>More Details</li>
                                                    </ul>
                                                </div>

                                            </div>
                                        </div>
                                    </a>
                                </div>
                            @endforeach
                        </div>

                    </div>



                </div>
            </div>
        </div>

    </div>

Front Controller

    public function productcategory($id)
    {
        $productcategory = ProductCategory::find($id);
        $products = Product::all();

        return view('pages.productcategory')
            ->with('productcategory', $productcategory)
            ->with('productcategories', ProductCategory::all())
            ->with('products', $products);
    }

Product.php Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    protected $fillable = [
        'title','content','product_category_id','featured','slug','link','price'
    ];

    public function getFeaturedAttribute($featured)
    {
        return asset($featured);
    }

    protected $dates = ['deleted_at'];

    public function productcategory()
    {
        return $this->belongsTo('App\ProductCategory');
    }

}

ProductCaategory.php Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProductCategory extends Model
{
    protected $fillable = [
        'name','icon','info','slug'
    ];

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

Please sign in or create an account to participate in this conversation.