Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4, eloquent - between statement and operators

There is a query I use to run in mysql :

select * from my_table where $val between col1 and coL2;

It works fine, but with laravel 4, the only way to make that query is to have something like

my_model::where('col1','>=',$val)->where('col2','<=',$val)

This way doesn't seem to work, because I don't have the same result when using the usual "select * ..."

Any idea ?

Just to clarify my request : In my case i dont have "...where column between value1 and value2" but "where value between commun" So it seems to me that i can't use "wherebetween"

like image 603
Shaft jackson Avatar asked Nov 27 '13 00:11

Shaft jackson


1 Answers

You may try something like this

// Get records whose id between 3 and 6
$users = User::whereBetween('id', array(3, 6))->get();

Or using variable

$id = 'id';
$from = 1;
$to = 5;
$users = User::whereBetween($id, array($from, $to))->get();

This will get all the records whose ID between 1 and 5.

like image 155
The Alpha Avatar answered Nov 02 '22 23:11

The Alpha