Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ashler2's avatar

Is there a better way to seperate data by date?

In my database, i have a due_date column, but for the frontend (Vue) I'm splitting it into 3; yesterday, today, tomorrow.

At the moment i've got the following in the controller.

    public function index()
    {
        $all_todos = Auth::user()->todos;

        $todos = [
            'yesterday' =>  $all_todos->where('due_date', Carbon::yesterday()->format('Y-m-d')),
            'today'     =>  $all_todos->where('due_date', Carbon::now()->format('Y-m-d')),
            'tomorrow'  =>  $all_todos->where('due_date', Carbon::tomorrow()->format('Y-m-d'))
        ];

        return Inertia::render('Dashboard', [
            'todos'     =>  $todos
        ]);
    }

Is there a way to split this out to be better, such as using a resource? I can't figure out a solution.

0 likes
2 replies
tykus's avatar

What is wrong with what you have; it is a relatively short Controller action that organises its information clearly, and passes it to the view?

You could optimise to ensure you are getting todo that are due within the three dates only:

$all_todos = Auth::user()->todos()
	->whereBetween(\DB::raw('DATE(due_date)', [Carbon::yesterday(), Carbon::tomorrow()])
->get();

Otherwise, you could try to get clever with the Collection using groupBy but the work required to get the keys computed correctly would not be worth the effort in my opinion.

ashler2's avatar

I suppose nothing is wrong with it, if i wanted i could seperate this out into a different function for formatting, but thats a little overkill. I've changed the query to be more efficent just wondering if the way forward would be to leave it in the controller or if there is a way to use a resource to format this.

Thanks for your help though!

Please or to participate in this conversation.