How to Create Multiple Where Clause Query Using Laravel Eloquent?

前端 未结 24 1382
一整个雨季
一整个雨季 2020-11-22 14:30

I\'m using the Laravel Eloquent query builder and I have a query where I want a WHERE clause on multiple conditions. It works, but it\'s not elegant.

E

24条回答
  •  一个人的身影
    2020-11-22 15:15

    Be sure to apply any other filters to sub queries, otherwise the or might gather all records.

    $query = Activity::whereNotNull('id');
    $count = 0;
    foreach ($this->Reporter()->get() as $service) {
            $condition = ($count == 0) ? "where" : "orWhere";
            $query->$condition(function ($query) use ($service) {
                $query->where('branch_id', '=', $service->branch_id)
                      ->where('activity_type_id', '=', $service->activity_type_id)
                      ->whereBetween('activity_date_time', [$this->start_date, $this->end_date]);
            });
        $count++;
    }
    return $query->get();
    

提交回复
热议问题