ordering dates

Published 1 month ago by dmcglone27

Hi all, I'm trying to get 3 dates, but I don't want any date that is more than 2 or 3 days in the past. For example I have Sept 29, 2016, Oct 15, 2017, Oct 16, 2017 and Oct 31, 2017.. All I want is the 15th, 16th and 31st of 2017 what could I do here to accomplish that? I have tried latest(), latest('date', 'asc') but neither work. Suggestions?

view()->composer('partials.events', function ($view) {

            $view->with('events', Events::orderBy('date)->where('archived', false)->limit(3)->get());

ejdelmonico

Try latest or oldest which orders by created_at column.

https://laravel.com/docs/5.5/queries#ordering-grouping-limit-and-offset

tisuchi
tisuchi
1 month ago (262,165 XP)

Doesn't it work for you?

$view->with('events', Events::orderBy('date', 'desc')->where('archived', false)->limit(3)->get());
Snapey
Snapey
1 month ago (683,015 XP)

you can only order if the column is datetime or timestamp. Is that true?

dmcglone27

Thanks guys, after a long hiatus from Laravel, I forgot a few things, but seeing the examples , refreshed my memory and here is what worked for me.

view()->composer('partials.events', function ($view) {


            $view->with('events', Events::orderBy('date')->where('date', '>=', Carbon::today())->where('archived', false)->limit(3)->get());

        });

Please sign in or create an account to participate in this conversation.