Wednesday, March 8, 2017

Convert MySQL search for a pair of values in a table to Eloquent query

I am having trouble reproducing this kind of a MySQL query using Eloquent

SELECT  *
    FROM    foo
    WHERE   (column1, column2) IN (('foo', 1), ('bar', 2))

There is a method in Eloquent query builder called whereIn(), but it can receive just one column as a parameter:

/**
 * Add a "where in" clause to the query.
 *
 * @param  string  $column
 * @param  mixed   $values
 * @param  string  $boolean
 * @param  bool    $not
 * @return $this
 */
public function whereIn($column, $values, $boolean = 'and', $not = false)
{ 
   ...
}

So, you can't do something like this

$qb = $this->model->whereIn(['column1', 'column2'], array([1, 2], [1,3], [3, 32]));

I am currently working very hard trying to find solution, but if anyone can help, I would be very grateful :)



via Matko Đipalo

Advertisement