-4

I'm a noob in Laravel. can anyone help me write this query in eloquent

SELECT
    *
FROM
    table
WHERE
    (
        STR_TO_DATE(`date`, '%m/%d/%Y') BETWEEN '2014-08-05'
        AND '2014-08-05'
    )

ORDER BY
    id
Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
Reza
  • 85
  • 1
  • 3
  • 10
  • 2
    If you don't want to learn - hire someone, as you do when your car is broken or when you have toothache. – zerkms Aug 07 '14 at 23:08
  • 1
    We are not here to do your work. Please read the manual and lets us see what you have tried. – bretterer Aug 07 '14 at 23:09

3 Answers3

2

If you want to use your query as is, just use DB::raw

http://laravel.com/docs/queries#raw-expressions

DB::raw(SELECT * FROM table WHERE ( STR_TO_DATE(date, '%m/%d/%Y') BETWEEN '2014-08-05' AND '2014-08-05' ) ORDER BY id);
2

Well making the assumption that your model is called Table, if your field is of type DATE, you can do this:

Table::where('date', '>=', '2014-08-05')
    ->where('date', '=<', '2014-08-05')
    ->get();

Alternatively you can do:

Table::select('table.*', DB::raw("STR_TO_DATE(date, '%m/%d/%Y') as date_format"))
    ->where('date_format', '>=', '2014-08-05')
    ->where('date_format', '=<', '2014-08-05')
    ->get();
ollieread
  • 6,018
  • 1
  • 20
  • 36
  • Just tested it on Laravel, it bring back: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'date_format' in 'field list' – Eran Levi Aug 08 '20 at 20:21
0

You may try this:

$from = '...';
$to = '...';

DB::table('table')->whereBetween('date', array($from, $to))->get();

Or using Eloquent:

ModelName::whereBetween('date', array($from, $to))->get();
The Alpha
  • 143,660
  • 29
  • 287
  • 307