Laravel - 比较日期

Im using Laravell framework. This a banner for events, only showing the events that exist after the current date.

This is the current function on routes.php

Route::get('/banner', function()
{
    $date = time();
    $events = Event::where('date', '>', $date)->get();
    return View::make('banner')->with('events', $events);
});

In the view, this shows all the events, doesnt matter if is before or after the current date. And if i do this change:

$events = Event::where('date', '<', $date)->get();

This dont show anything.

The view is working good, my main problem here is that i cant find how to fix this.

You can do this.

In your Event model add the following line.

protected $dates = ['date'];

By doing it when Event get saved the date will be saved as a Carbon instance.

Then when you query you could simply do this.

$events = Event::where('date', '>', Carbon::now())->get();

make sure you import Carbon like this.

use Carbon\Carbon;

Refer Fun with Dates by Laracasts for more details.