试图获取非对象的属性“ parent_id”

时间:2019-01-15 08:22:12

标签: laravel eloquent laravel-5.7

我正在尝试从管理面板获取网站的类别(包含产品)到网站的标题,但它仅显示一个类别。当我单击它以查看其相关产品时,会收到错误消息。

  

试图获取非对象的属性“ parent_id”。

错误在这一行:

if($categoryDetails->parent_id==0){

ProductsController

<?php

public function products($url = null)
{
    $categories = Category::with('products')->where(['parent_id' => 0])->get();
    $categoryDetails = Category::where(['url' => $url])->first();

    if ($categoryDetails->parent_id == 0) {
        //if url is main category url
        $subCategories = Category::where(['parent_id' => $categoryDetails])->get();
        $cat_ids = "";
        foreach ($subCategories as $subCat) {
            $cat_ids .= $subcat->id . ",";
        }

        $productsAll = Product::whereIn('category_id', array($cat_ids))->get();
    } else {
        //if url is sub category url
        $productsAll = Product::where(['category_id' => $categoryDetails->id])->get();
    }

    return view('products.listing')->with(compact('categories', 'categoryDetails', 'productsAll'));
}

listing.blade.php (我编写此代码是为了将其与“类别”相关联)

{{ $categoryDetails->name }}

<!--the below code is working in index.blade.php and here as well
@foreach($productsALL as $product)
    {{ $product->product_name }}
@endforeach-->

0 个答案:

没有答案