select *
from tr_sewaiklan
where '2018-12-09' BETWEEN tgl_mulai AND tgl_selesai
Asked
Active
Viewed 49 times
-3

Alon Eitan
- 11,997
- 8
- 49
- 58

danitri
- 11
- 1
- 7
-
You should add code of your model and the code of what you have tried so far. – Laerte Dec 09 '18 at 10:40
-
https://stackoverflow.com/questions/26746918/laravel-eloquent-how-to-use-between-operator – Jane Dec 09 '18 at 10:44
-
nope dude, because whereBetween format is field_date and then array value, but in my query format is value_date and then field_date... – danitri Dec 09 '18 at 11:23
3 Answers
1
DB::tabe('tr_swaiklan')->where('2018-12-09', '>', $tgl_mulai)->where('2018-12-09', '<', $tgl_selesai)->get();
the <>
Operator might be used too
it's quite well described in the official documentation: https://laravel.com/docs/5.7/queries#where-clauses

Simon
- 1,280
- 3
- 10
- 21
-
thanks for help but your query doesnt work.. but finally i use raw expression – danitri Dec 09 '18 at 11:24
1
See Laravel Eloquent how to use between operator
to test in tinker without model, try
\DB::table('table')->whereBetween('date_field', ['2018-12-09', '2019-01-01'])->get();
from comment
DB::table('tr_sewaiklan')->where(function ($query) {
$query
->where('tgl_mulai', '<', '2018-12-09')
->where('tgl_selesai', '>', '2018-12-09');
});

Jane
- 428
- 3
- 10
-
nope dude, because whereBetween format is field_date and then array value, but in my query format is value_date and then field_date... – danitri Dec 09 '18 at 11:23
-
Sorry, misread your question, my bad. If you want an alternative to raw queries you could use a where closure, something like DB::table('tr_sewaiklan')->where(function ($query) { $query ->where('tgl_mulai', '<', '2018-12-09') ->where('tgl_selesai', '>', '2018-12-09'); }); If your date is really a variable you `use ()` it in the closure – Jane Dec 09 '18 at 22:24
0
Finally i solved my problem with raw expression not with eloquent because whereBetween format is field_date and then value_date but in my case my query format is value_date and then field_date.
thanks for your help guys..
DB::select(DB::raw("SELECT a.* FROM tr_sewaiklan a LEFT JOIN tb_iklanposisi b ON a.id_posisi=b.id WHERE '".date('Y-m-d')."' BETWEEN tgl_mulai AND tgl_selesai"));

danitri
- 11
- 1
- 7
-
`date('Y-m-d')` - Why not just use [CURDATE()](https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_curdate)? – Alon Eitan Dec 09 '18 at 12:58