How to Create Multiple Where Clause Query Using Laravel Eloquent?

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

    You can use eloquent in Laravel 5.3

    All results

    UserModel::where('id_user', $id_user)
                    ->where('estado', 1)
                    ->get();
    

    Partial results

    UserModel::where('id_user', $id_user)
                        ->where('estado', 1)
                        ->pluck('id_rol');
    
    0 讨论(0)
  • 2020-11-22 15:14
    DB::table('users')
                ->where('name', '=', 'John')
                ->orWhere(function ($query) {
                    $query->where('votes', '>', 100)
                          ->where('title', '<>', 'Admin');
                })
                ->get();
    
    0 讨论(0)
  • 2020-11-22 15:15

    In Laravel 5.3 (and still true as of 7.x) you can use more granular wheres passed as an array:

    $query->where([
        ['column_1', '=', 'value_1'],
        ['column_2', '<>', 'value_2'],
        [COLUMN, OPERATOR, VALUE],
        ...
    ])
    

    Personally I haven't found use-case for this over just multiple where calls, but fact is you can use it.

    Since June 2014 you can pass an array to where

    As long as you want all the wheres use and operator, you can group them this way:

    $matchThese = ['field' => 'value', 'another_field' => 'another_value', ...];
    
    // if you need another group of wheres as an alternative:
    $orThose = ['yet_another_field' => 'yet_another_value', ...];
    

    Then:

    $results = User::where($matchThese)->get();
    
    // with another group
    $results = User::where($matchThese)
        ->orWhere($orThose)
        ->get();
    

    The above will result in such query:

    SELECT * FROM users
      WHERE (field = value AND another_field = another_value AND ...)
      OR (yet_another_field = yet_another_value AND ...)
    
    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2020-11-22 15:15
    public function search()
    {
        if (isset($_GET) && !empty($_GET))
        {
            $prepareQuery = '';
            foreach ($_GET as $key => $data)
            {
                if ($data)
                {
                    $prepareQuery.=$key . ' = "' . $data . '" OR ';
                }
            }
            $query = substr($prepareQuery, 0, -3);
            if ($query)
                $model = Businesses::whereRaw($query)->get();
            else
                $model = Businesses::get();
    
            return view('pages.search', compact('model', 'model'));
        }
    }
    
    0 讨论(0)
  • 2020-11-22 15:16

    Conditions using Array:

    $users = User::where([
           'column1' => value1,
           'column2' => value2,
           'column3' => value3
    ])->get();
    

    Will produce query like bellow:

    SELECT * FROM TABLE WHERE column1 = value1 and column2 = value2 and column3 = value3
    

    Conditions using Anonymous Function:

    $users = User::where('column1', '=', value1)
                   ->where(function($query) use ($variable1,$variable2){
                        $query->where('column2','=',$variable1)
                       ->orWhere('column3','=',$variable2);
                   })
                  ->where(function($query2) use ($variable1,$variable2){
                        $query2->where('column4','=',$variable1)
                       ->where('column5','=',$variable2);
                  })->get();
    

    Will produce query like bellow:

    SELECT * FROM TABLE WHERE column1 = value1 and (column2 = value2 or column3 = value3) and (column4 = value4 and column5 = value5)
    
    0 讨论(0)
提交回复
热议问题