Home > Blockchain >  How to minimize "where()" on Laravel query builder. If all table columns are all the same
How to minimize "where()" on Laravel query builder. If all table columns are all the same

Time:12-08

Is there a way to minimize calling ->where() on laravel query builder?

Sample Table:

name item1 item2 item 3
David 0 0 0
David 1 1 2
David 1 2 2

I want to remove a specific record with all the table columns containing 0 value without removing the other records.

My query is this.

DB::table('users')->where('name', 'David')
                  ->where('item1', 0)
                  ->where('item2', 0)
                  ->where('item3', 0)
                  ->delete();

Is there a way to minimize calling->where()? Just like in whereIn('columns',[1,2,3,4]) but in columns.

CodePudding user response:

Pass an array of conditions to the where function. Each element of the array should be an array containing the three arguments typically passed to the where method:

$conditions=[
  ['name','=', 'David'],
  ['item1','=',0],
  ['item2','=', 0],
  ['item3','=', 0],
]
DB::table('users')->where($conditions)->delete();

Ref: https://laravel.com/docs/8.x/queries#where-clauses

  • Related