How to get a list of events from the current date?

1

Actually, I have the solution, but I want to know if there is a more efficient solution. What I need are the events that have not yet expired so to speak. Within a base with all the events.

Basically what I do is get the events with an OrderBy from the ascending date and then I go through, comparing with the current one, taking the minor ones. And staying with those that exceed the current date.

I wanted to know if in any way, from a controller or a composer, I can already get the list with the date I need from the beginning, without using the foreach. It would be neat, or efficient too, somehow put a query to the database here

public function compose(View $view)
{
    $categorias= Categoria::orderBy('nombre','asc')->get();
    $tags= Tag::orderBy('nombre','asc')->get();
    $eventos= Evento::orderBy('fecha','asc')->get();

    foreach ($eventos as $key => $evento) {
        if ($evento->fecha <= Carbon::now()) {
             unset($eventos[$key]);
        }
    }

    $view
        ->with('categorias', $categorias)
        ->with('eventos',$eventos)
        ->with('tags',$tags);
}
    
asked by Cidius 07.10.2016 в 16:13
source

1 answer

0

I self-answer in case someone serves you later.

public function compose(View $view)
{
    $categorias= Categoria::orderBy('nombre','asc')->get();
    $tags= Tag::orderBy('nombre','asc')->get();
    //$eventos= Evento::orderBy('fecha','asc')->get();

    $eventos = Evento::where('fecha', '>=', Carbon::now())
            ->orderBy('fecha', 'asc')
            ->get();

    $view
        ->with('categorias', $categorias)
        ->with('eventos',$eventos)
        ->with('tags',$tags);
}

In this way, we released the foreach and it already brings us a collection with the events with the restriction I needed

    
answered by 07.10.2016 / 22:23
source