Laravel (hasManyThrough eloquent relation) – WhereHas with OrderBy working on localhost, not working on server

huangapple go评论68阅读模式
英文:

Laravel (hasManyThrough eloquent relation) - WhereHas with OrderBy working on localhost, not working on server

问题

Laravel(hasManyThrough eloquent relation)- 在本地环境正常工作,但在服务器上不正常工作。在本地环境中,它可以正常排序,但在生产服务器上无法正确排序。
在本地环境中使用的是MARIADB,在生产服务器上使用的是MYSQL。

public function index(Product $product)
{
    $products = Product::with(['subcategory', 'subcategory.category'])->whereHas('subcategory', function ($query) {
        $query->whereHas('category', function ($query) {
            $query->orderBy('category_id', 'asc');
        });
    })->get();

    return view('products.index', compact('products'));
}
英文:

Laravel (hasManyThrough eloquent relation) - WhereHas with OrderBy working on localhost, not working on server. On localhost it working, it sorting (ordering) well, but on live server dont ordering well.
On localhost is MARIADB on live server is MYSQL.

public function index(Product $product)

{
 $products = Product::with(['subcategory', 'subcategory.category'])->whereHas('subcategory', function ($query) {
     $query->whereHas('category', function ($query) {
     $query->orderBy('category_id', 'asc');
 }); })->get();

  return view('products.index', compact('products'));
}

答案1

得分: 1

更改您的关系如下:

public function brands()
{
   return $this->hasManyThrough('App\Brand', 'App\Product', 'category_id', 'id', 'brand_id');
}
英文:

change your relationship as below:

public function brands()
{
   return $this->hasManyThrough('App\Brand', 'App\Product' 'category_id','id','brand_id');
}

答案2

得分: 0

尝试这样做:

$products = Product::with(['subcategory', 'subcategory.category'])
    ->has('subcategory.category')->orderBy('category_id', 'asc')->get();
return view('products.index', compact('products'));
英文:

Try this

 $products = Product::with(['subcategory', 'subcategory.category'])
    ->has('subcategory.category')->orderBy('category_id', 'asc')->get();
 return view('products.index', compact('products'));

huangapple
  • 本文由 发表于 2020年1月6日 19:56:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定