How to Do Model->Where('Id', Array) Multiple Where Conditions

How to Create Multiple Where Clause Query Using Laravel Eloquent?

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 ...)

multiple where condition codeigniter

you can use an array and pass the array.

Associative array method:
$array = array('name' => $name, 'title' => $title, 'status' => $status);

$this->db->where($array);

// Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'

Or if you want to do something other than = comparison

$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);

$this->db->where($array);

array for multiple where_in condition in codeigniter

function select_in($arr)
{
$this->db->select('*');
$this->db->from('table');
$this->db->where($arr); // change here
$query = $this->db->get();
return $query;
}

If you want multiple where In then you need to write it twice....It's not possible in single statement.

$this->db->where_in('field1',$cond1);
$this->db->where_in('field2' , $cond2);

Note: Where_in is similar to where id IN (1,2,3...)but in your case you are doing multiple where condition.

javascript filter array multiple conditions

You can do like this

var filter = {  address: 'England',  name: 'Mark'};var users = [{    name: 'John',    email: 'johnson@mail.com',    age: 25,    address: 'USA'  },  {    name: 'Tom',    email: 'tom@mail.com',    age: 35,    address: 'England'  },  {    name: 'Mark',    email: 'mark@mail.com',    age: 28,    address: 'England'  }];

users= users.filter(function(item) { for (var key in filter) { if (item[key] === undefined || item[key] != filter[key]) return false; } return true;});
console.log(users)

laravel 5, where clause with multiple values

You need to use whereIn(), and maybe a better option for the whole deal would be:

$ids = DB::table('requests')->where('id', $x)->lists('id');
$friend = DB::table('users')->whereIn('id', $ids)->get();

Note that the second parameter of whereIn() needs to be an array.

Laravel eloquent multiple WHERE with OR AND OR and LIKE?

Use like and not like with % in where() and orWhere() methods:

->where('column', 'like', '%pattern%')

https://laravel.com/docs/5.3/queries#where-clauses

If you need to use multiple AND do like this:

->where($condition)
->where($anotherCondition)

If you want to use OR do this:

->where($condition)
->orWhere($anotherCondition)

To combine multiple AND and OR do parameter grouping:

->where('name', '=', 'John')
->orWhere(function ($query) {
$query->where('votes', '>', 100)
->where('title', '<>', 'Admin');
})

How do I chain multiple where and orWhere clause in laravel from an Array

As per the Laravel Documentation you should be able to use whereIn() or whereNotIn() with an array.

$users = DB::table('users')
->whereIn('id', array(1, 2, 3))->get();

$users = DB::table('users')
->whereNotIn('id', array(1, 2, 3))->get();

So for your example

$query->whereIn('lga',$user_lgas)->get();

For more details see: Database: Query Builder



Related Topics



Leave a reply



Submit