5

Here is my code for selecting reservations:

Reservation::select(array(
    'obj_id', 
    'name', 
    'surname',
    'date_from',
    'date_to', 
    'days',
    'status',
    'slug',
    'payable'
));

My question is how to change 'days' to get real diff between date_from and date_to? I don't have 'days' column in the database, I just want to count them in the SQL query.

In pure SQL should be something like that:

SELECT DATEDIFF('date_from, date_to) AS Days FROM RESERVATIONS;

How can I do that using Eloquent?

Rubens Mariuzzo
  • 28,358
  • 27
  • 121
  • 148
Maciej Płocki
  • 362
  • 2
  • 7
  • 18

1 Answers1

16

You can use DB::raw for this.

Reservation::select(array(
            'obj_id', 'name', 'surname',
            'date_from', 'date_to', 
            'days',
            'status', 'slug', 'payable', 
            DB::raw("DATEDIFF(date_from,date_to)AS Days"))
))
Abishek
  • 11,191
  • 19
  • 72
  • 111