Sort data in a laravel view

1

I have a query, it turns out that in my Events handler I receive all the events of a teacher but I would like to sort them by date

This is the index of my controller

public function index(Request $request)
{   
    $profesor = Profesor::find(auth('profesor')->user()->id);
    $mis_eventos = $profesor->eventos->all();
    return view('eventos.index')->with('mis_eventos',$mis_eventos)->with('profesor',$profesor)->with('i', ($request->input('page', 1) - 1) * 5);
}

In what part should I place the order by?

    
asked by Edgardo Escobar 31.03.2017 в 23:26
source

2 answers

0

When you define $profesor , you can do eager loading of your events directly, without needing to define an additional variable:

$profesor = Profesor::find(...)->with('eventos')->get();

You can preload the order by defining it in the relationship, in your model:

public function eventos()
{
    return $this->hasMany(Evento::class)->orderBy('fecha');
}
    
answered by 01.04.2017 / 00:58
source
-1

Try doing the following in your code:

$mis_eventos = $profesor->eventos->orderBy('campo_de_fecha')->all();
    
answered by 01.04.2017 в 00:30