eloquent仅适用于if不为null的情况

时间:2017-11-30 12:38:16

标签: php laravel eloquent

我有以下查询链接的地方:

->where('start_timestamp', '<=', $activity->timestamp)
->where('end_timestamp', '>=', $activity->timestamp)
->where('minimum_activity_distance', '<=', $activity->distance)
->where('maximum_activity_distance', '>=', $activity->distance)
->where('minimum_activity_duration', '<=', $activity->duration)
->where('maximum_activity_duration', '>=', $activity->duration)
->where('minimum_activity_timestamp_time', '<=', $activity->timestamp->format('H:i:s'))
->where('maximum_activity_timestamp_time', '>=', $activity->timestamp->format('H:i:s'));

但是,列也可能为NULL。如果是这样,我不希望条件适用。

所以如果minimum_activity_distance为NULL 我想跳过/忽略

->where('minimum_activity_distance', '<=', $activity->distance)

1 个答案:

答案 0 :(得分:1)

您可以将whereclosure用于此

   ->where(function ($q) use ($activity) {
       $q->where('minimum_activity_distance', '<=', $activity->distance)
          ->orWhereNull('minimum_activity_distance');
    })