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

时间:2017-10-31 01:00:48

标签: php laravel

您好试着在子类别页面中获取我的产品列表,但我收到此错误

  

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

这是我的功能

// 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'));
    }

这是我的路线:

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

我的产品型号:

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

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

类别模型:

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

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

子类别模型:

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

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

任何想法?

更新

我将我的功能改为:

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

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

更新2

我将我的功能改为:

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

现在我可以获得产品,但问题是所有产品都会显示出来   如果他们被包括在其他类别中。

如何解决?

2 个答案:

答案 0 :(得分:0)

我不明白你的意思

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

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

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

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

关系名称必须与Product模型

中的关系方法相同

答案 1 :(得分:0)

解决

  $.ajax({
    url: url,
    type: "GET",
    headers: {
      "Accept": "application/xml;"
    },
    success: function(data, textStatus, xhr) {
      alert(textStatus);
      var xml = data;
      var table;

      var users = xml.getElementsByTagName("character");
      for (var i = 0; i < users.length; i++) {
        var user = users[i];

        var names = user.getElementsByTagName("name");
        var name = user.getElementsByTagName("real_name")
        for (var j = 0; j < names.length; j++) {
          alert(names[j].childNodes[0].nodeValue);

        }

      }
      document.getElementById("results").innerHTML = table;
    },
    error: function(data, textStatus, xhr) {
      alert("error occured\n" data.message);
    }
  });

public function productsubcategory($slug, $subslug) { $products = Product::whereHas('subcategory', function($q) use($subslug){ $q->where('slug',$subslug); })->get(); return view('frontend.subcategories', compact('products')); } 的产品型号更改为subcategories