php - Laravel Eloquent - 过滤器关系

标签 php laravel eloquent

我的 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/

相关文章:

php - 在生产环境中认真使用 debug_backtrace() 是否安全?

php - 使用 Kannel 发送短信需要什么?

php - Laravel Elixir 没有结合脚本

Laravel Eloquent,附加与关系同名的属性

php - Laravel 5.4 在数据透视表中插入时调用 null 上的成员函数 Attach()

php - Laravel 集合 : extract specified set of keys and set the non-existing keys to a default value

PHP简单代理

php - mysqli 准备好的语句返回 0 行,没有错误

php - Laravel Input::hasFile ('image' ) 即使文件已上传也返回 false

php - 如何在 helper 类中使用 laravel Eloquent 模型