I am using Laravel Scout to search a model, and then I'm using a filter to get past events and upcoming events.
Here is my method:
public function search(Request $request)
{
$search = $request->get('q');
// Laravel Scout search() method
$users = User::search($search)->get();
$articles = Article::search($search)->get();
$events = Event::search($search)->get();
$today = Carbon::now();
$upcomingEvents = $events->filter(function ($events) use ($today) {
return Carbon::parse($events->startDate)->gt($today);
});
$pastEvents = $events->filter(function ($events) use ($today) {
return Carbon::parse($events->startDate)->lt($today);
});
$userCount = count($users);
$articleCount = count($articles);
$eventCount = count($events);
$upcomingEventCount = count($upcomingEvents);
$pastEventCount = count($pastEvents);
$templateCount = 0;
return view('pages.search.index', compact('search', 'users', 'articles', 'upcomingEvents', 'pastEvents', 'userCount', 'articleCount', 'upcomingEventCount', 'pastEventCount', 'templateCount'));
}
I had read a tutorial here: https://scotch.io/tutorials/easier-datetime-in-laravel-and-php-with-carbon
Reading this I saw you can get today, right now, with Carbon::now().
There's also lt() for less than and gt() for greater than.
In my filter I've grabbed the start date and compared it to Carbon::now() but for some reason, it does not return what i expect.
In my database startDate is a VARCHAR but in my Event model I have specified the following:
protected $dates = [
'startDate', 'finishDate'
];
This is my database table:
But in my view I get this:
Am I using Carbon wrongly?
via Chebli Mohamed


Aucun commentaire:
Enregistrer un commentaire