Laravel orWhere 与左连接

Laravel orWhere with left join

我不太擅长Laravel Eloquent,所以请保持冷静。

我有 1 个产品 table 和 4 个“目标”table(销售、新颖性、清算和曝光)。

我正在尝试通过此查询获取至少具有四个“目标”之一的所有产品:

return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
        ->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
        ->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
        ->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
        ->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
        ->where('noveltys.novelty_end_at', '!=', null)
        ->orwhere('sales.sale_end_at', '!=', null)
        ->orWhere('liquidations.liquidation_end_at', '!=', null)
        ->orWhere('exposures.exposure_end_at', '!=', null)
        ->get();

问题是,只有 return 个产品的第一个“where”子句为真。 (换句话说,目前,此查询仅 return 具有 not null novelty_end_at 的产品,它不会 return 具有 sale_end_at 和其他产品的产品。

我怎样才能实现 return 具有非空 sale_end_atexposure_end_atliquidation_end_at 的产品?

谢谢!

似乎是语法错误

->orwhere('sales.sale_end_at', '!=', null)

应该是

->orWhere('sales.sale_end_at', '!=', null)

您也可以尝试使用 orWhereNotNull

喜欢

return Product::leftjoin('sales', 'sales.product_id', '=', 'products.id')
        ->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
        ->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
        ->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
        ->select('products.*', 'sales.*', 'liquidations.*', 'noveltys.*', 'exposures.*')
        ->whereNotNull('noveltys.novelty_end_at')
        ->orWhereNotNull('sales.sale_end_at')
        ->orWhereNotNull('liquidations.liquidation_end_at')
        ->orWhereNotNull('exposures.exposure_end_at')
        ->get();

我找到了解决方案,它几乎是同一回事,除了我正在使用 orWhereNotNull 并且我在开始时使用 DB::table 而不是直接使用 Product::leftjoin

 return DB::table('products')
            ->leftjoin('sales', 'sales.product_id', '=', 'products.id')
            ->leftjoin('liquidations', 'liquidations.product_id', '=', 'products.id')
            ->leftjoin('noveltys', 'noveltys.product_id', '=', 'products.id')
            ->leftjoin('exposures', 'exposures.product_id', '=', 'products.id')
            ->whereNotNull('sales.sale_end_at')
            ->orwhereNotNull('noveltys.novelty_end_at')
            ->orwhereNotNull('liquidations.liquidation_end_at')
            ->orwhereNotNull('exposures.exposure_end_at')
            ->get();

谢谢!

也许您可以使用 ->orWhereHas() 方法。要使用这个函数,你必须在你的Sales和四个target中定义关系Model类。文档:https://laravel.com/docs/9.x/eloquent-relationships

然后在您的查询中,

return Product::whereHas('noveltys', function(Builder $query) {
                  // your where clause
              })
              ->orWhereHas('sales', function(Builder $query) {
                  // your where clause
              })
              ->orWhereHas('liquidations', function(Builder $query) {
                  // your where clause
              })
              ->orWhereHas('exposures', function(Builder $query) {
                  // your where clause
              })
              ->get();