gpt4 book ai didi

php - Laravel Eloquent - 过滤器关系

转载 作者:行者123 更新时间:2023-12-04 13:26:24 25 4
gpt4 key购买 nike

我的 Laravel 项目中有两个主要模型。 ProductMaster 是主要产品数据所在的位置,而 Product 是保存每个产品的变化和每个客户的价格的模型。我不允许更改此模型,以防万一。
我的问题是如何进行 Eloquent 查询以获取包含由客户端(以及其他参数)过滤的产品的 ProductMaster 数据。我试过 whereHas 但没有用。
这是我的模型:

namespace App\Models;
use Illuminate\Database\Eloquent\Model;

class ProductMaster extends Model
{
protected $table = 'product_masters';

protected $primaryKey = 'id';

protected $fillable = ['code','title'];

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


namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use App\Models\ProductMaster;

class Product extends Model
{

protected $table = 'products';
protected $primaryKey = 'id';

protected $fillable = ['sku', 'stock', 'brand', 'size', 'priceList', 'master_id', 'client'];

public function productMaster(){
return $this->belongsTo(ProductMaster::class,'master_id');
}

}
这是我试图做的查询:
        //QUERY WITH FILTERS
$products = ProductMaster::whereHas('products', function($query)
use($filterBrand, $filterCats, $filterSizes, $clientCode)
{
$query->where('client', $clientCode);
$query->whereIn('brand', $filterBrand);
$query->whereIn('size', $filterSizes);
})
->where('title', 'LIKE', '%' . $request->find . '%')
->orderby($order[0], $order[1])
->paginate(6)
->withQueryString();
这个查询有效,但我没有得到我需要的。这给了我所有具有具有该参数的产品的 ProductMaster,但在集合 $products 中,它放置了具有该 master_id 的所有产品,而不仅仅是具有该参数的产品。
这是sql:
select * from `product_masters` where exists (select * from `products` where `product_masters`.`id` = `products`.`master_id` and `client` = ? and `products`.`brand` in (?) and `category` in (?) and `client` = ?) and `title` LIKE ? order by `title` asc
以下是一些示例数据: SQL Fiddle
任何人都可以帮助我吗?
谢谢。

最佳答案

如果您想过滤要预先加载的产品并过滤以仅加载拥有这些产品的产品母版,您的查询应该是这样的:


$products = ProductMaster::
with([
'products' =>
fn($query) => $query
->where('client', $clientCode)
->whereIn('brand', $filterBrand)
->whereIn('size', $filterSizes)
])
->whereHas('products',
fn($query) => $query
->where('client', $clientCode)
->whereIn('brand', $filterBrand)
->whereIn('size', $filterSizes)
)
->where('title', 'LIKE', '%' . $request->find . '%')
->orderby($order[0], $order[1])
->paginate(6)
->withQueryString();
您甚至可以将 with 和 whereHas 函数的查询传递给 Controller ​​内的私有(private)函数,以保持克隆更干净。

关于php - Laravel Eloquent - 过滤器关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68309271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com