How to Create Multiple Where Clause Query Using Laravel Eloquent?

前端 未结 24 1081
一整个雨季
一整个雨季 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:07
    $projects = DB::table('projects')->where([['title','like','%'.$input.'%'],
        ['status','<>','Pending'],
        ['status','<>','Not Available']])
    ->orwhere([['owner', 'like', '%'.$input.'%'],
        ['status','<>','Pending'],
        ['status','<>','Not Available']])->get();
    
    0 讨论(0)
  • 2020-11-22 15:08

    In this case you could use something like this:

    User::where('this', '=', 1)
        ->whereNotNull('created_at')
        ->whereNotNull('updated_at')
        ->where(function($query){
            return $query
            ->whereNull('alias')
            ->orWhere('alias', '=', 'admin');
        });
    

    It should supply you with a query like:

    SELECT * FROM `user` 
    WHERE `user`.`this` = 1 
        AND `user`.`created_at` IS NOT NULL 
        AND `user`.`updated_at` IS NOT NULL 
        AND (`alias` IS NULL OR `alias` = 'admin')
    
    0 讨论(0)
  • 2020-11-22 15:08

    The whereColumn method can be passed an array of multiple conditions. These conditions will be joined using the and operator.

    Example:

    $users = DB::table('users')
                ->whereColumn([
                    ['first_name', '=', 'last_name'],
                    ['updated_at', '>', 'created_at']
                ])->get();
    
    $users = User::whereColumn([
                    ['first_name', '=', 'last_name'],
                    ['updated_at', '>', 'created_at']
                ])->get();
    

    For more information check this section of the documentation https://laravel.com/docs/5.4/queries#where-clauses

    0 讨论(0)
  • 2020-11-22 15:08

    Without a real example, it is difficult to make a recommendation. However, I've never needed to use that many WHERE clauses in a query and it may indicate a problem with the structure of your data.

    It may be helpful for you to learn about data normalization: http://en.wikipedia.org/wiki/Third_normal_form

    0 讨论(0)
  • 2020-11-22 15:10

    With Eloquent it is easy to create multiple where check:

    First: (Use simple where)

    $users = User::where('name', $request['name'])
        ->where('surname', $request['surname'])
        ->where('address', $request['address'])
        ...
        ->get();
    

    Second: (Group your where inside an array)

    $users = User::where([
        ['name', $request['name']],
        ['surname', $request['surname']],
        ['address', $request['address']],
        ...
    ])->get();
    

    You can also use conditional (=, <>, etc.) inside where like this:

    $users = User::where('name', '=', $request['name'])
        ->where('surname', '=', $request['surname'])
        ->where('address', '<>', $request['address'])
        ...
        ->get();
    
    0 讨论(0)
  • 2020-11-22 15:10

    You can use array in where clause as shown in below.

    $result=DB::table('users')->where(array(
    'column1' => value1,
    'column2' => value2,
    'column3' => value3))
    ->get();
    
    0 讨论(0)
提交回复
热议问题