调用 Laravel 中未定义的方法 App\Product::whereHas()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47026412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:53:02  来源:igfitidea点击:

Call to undefined method App\Product::whereHas() in laravel

phplaravel

提问by mafortis

Hi try to get my products list in subcategory page but i'm getting this error

嗨,尝试在子类别页面中获取我的产品列表,但出现此错误

Call to undefined method App\Product::whereHas()

调用未定义的方法 App\Product::whereHas()

here is my function

这是我的功能

// Subategory page included posts
    public function productsubcategory($slug)
    {
       $products = Product::whereHas('subcategory.category', function($q) use($slug){
       $q->where('slug',$slug);
       })->paginate(6);
        return view('frontend.subcategories', compact('products'));
    }

this is my route:

这是我的路线:

Route::get('/category/{slug}/subcategory/{subslug}', ['as' => 'showbysub', 'uses' => 'IndexController@productsubcategory']);

my product model:

我的产品型号:

public function categories(){
     return $this->belongsTo(Category::class);
  }

  public function subcategories(){
     return $this->belongsTo(Subcategory::class);
  }

category model:

类别型号:

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

  public function subcategories(){
     return $this->hasMany(Subcategory::class);
  }

subcategory model:

子类模型:

public function category(){
     return $this->belongsTo(Category::class, 'category_id');
  }

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

any idea?

任何的想法?

UPDATE:

更新:

I changed my function to:

我将我的功能更改为:

public function productsubcategory($slug)
    {
        $subcategories = Subcategory::where('slug','=',$slug)->with('products')->paginate(6);
        return view('frontend.subcategories', compact('subcategories'));
    }

and now my page is loading but will not show any item (product).

现在我的页面正在加载但不会显示任何项目(产品)。

UPDATE 2

更新 2

I changed my function to this:

我把我的功能改成这样:

public function productsubcategory($slug)
    {
        $products = Product::with('subcategories')
        ->orderBy('id', 'desc')
        ->get();
        return view('frontend.subcategories', compact('products'));
    }

Now I can get products but the problem is all products will show even if they're included other categories.

现在我可以获得产品,但问题是所有产品都会显示,即使它们包含在其他类别中。

How to solve that?

如何解决?

采纳答案by mafortis

SOLVED

解决了

public function productsubcategory($slug, $subslug)
    {
        $products = Product::whereHas('subcategory', function($q) use($subslug){
        $q->where('slug',$subslug);
        })->get();
        return view('frontend.subcategories', compact('products'));
    }

and product model from subcategorieschange to subcategory

和产品型号从subcategories变化到subcategory

回答by Nyicip

I don't understand what do you mean in this lines

我不明白你在这行里的意思

$products = Product::whereHas('subcategory.category', function($q) use($slug){
    $q->where('slug',$slug);
})->paginate(6);

Refer to https://laravel.com/docs/5.4/eloquent-relationships. If you are trying to querying relationship, you can do like this:

请参阅https://laravel.com/docs/5.4/eloquent-relationships。如果您正在尝试查询关系,您可以这样做:

$products = Product::whereHas('subcategories', function($q) use($slug){
    $q->where('slug',$slug);
})->paginate(6);

or

或者

$products = Product::whereHas('categories', function($q) use($slug){
    $q->where('slug',$slug);
})->paginate(6);

The relationship name must the same with relationship method in your Productmodel

关系名称必须与Product模型中的关系方法相同