How to Create Multiple Where Clause Query Using Laravel Eloquent?

前端 未结 24 1129
一整个雨季
一整个雨季 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:31

    A sample of code.

    Firstly :

    $matchesLcl=[];
    

    array gets filled here using desired count / loop of conditions, incremently :

     $matchesLcl['pos']= $request->pos;
    $matchesLcl['operation']= $operation;
    //+......+
    $matchesLcl['somethingN']= $valueN;
    

    and further with eloquents like this shrink expression :

    if (!empty($matchesLcl))
        $setLcl= MyModel::select(['a', 'b', 'c', 'd'])
            ->where($matchesLcl)
            ->whereBetween('updated_at', array($newStartDate . ' 00:00:00', $newEndDate . ' 23:59:59'));
    else 
        $setLcl= MyModel::select(['a', 'b', 'c', 'd'])
            ->whereBetween('updated_at', array($newStartDate . ' 00:00:00', $newEndDate . ' 23:59:59'));
    

提交回复
热议问题