orderBy in laravel

1

I have a driver that sends a list of enrollments by subject, each registration belongs to a student.

public function showCalificacionesAsignatura($id)
{
    $asignatura = Asignatura::find($id);
    $matriculados = $asignatura->matriculas;

At the hearing I receive the registration and use your relationship with the student to show their data:

@foreach ($matriculados as $m)
            <tr>
                <td>{{$m->alumno->apellido_paterno." ".$m->alumno->apellido_materno." ".$m->alumno->nombre}}</td>

How can I sort these student names with orderBy ('parental_name', 'DESC')?

model

Student Model

public function matriculas()
{
    return $this->hasMany('App\Matricula','id_alumno');
}

Registration Model

public function alumno()
{
    return $this->belongsTo(Alumno::class,'id_alumno','id')->orderBy('apellido_paterno','DESC');
}

public function curso()
{
    return $this->belongsTo(Curso::class,'id_curso','id');
}

public function notas()
{
    return $this->hasMany('App\Nota','id_matricula');
}

public function asignaturas() 
{
    return $this->belongsToMany('App\Asignatura')->orderBy('nombre','ASC');
}

Subject Model

public function matriculas() 
{
    return $this->belongsToMany('App\Matricula')->withTimestamps(); 
}
    
asked by Edgardo Escobar 20.06.2017 в 05:05
source

2 answers

0

If you're always going to want the relationship to be sorted by last name, you can add the order by to that relationship

I leave this example, as you have not put the model, it probably does not fit, but it will give you the idea of how to do it

/* relación dentro del modelo */
public function matriculas() 
{
    return $this->belongsToMany('App\Matricula')->orderBy('apellido_paterno', 'DESC');
}

Another solution without much complications is to make the consultation more direct:

$matriculados = Alumno::select( 'Asignatura.*', 'Matricula.*', 'Alumno.*' )
->join( 'Matriculas', 'Alumno.idalumno', '=', 'Matricula.idalumno' )
->join( 'Asignatura', 'Matricula.idasignatura', '=', 'Asignatura.idasignatura' )
->where('Asignatura.idasugnatura', '=', $id )
->orderBy('Alumno.apellido_paterno', 'desc')
->get();
    
answered by 20.06.2017 / 16:01
source
1

You should preload the relationship with Eager Loading and make the query to sort it in this, something like this:

$asignatura = Asignatura::with(['matriculas' => function ($query) {
    $query->orderBy('apellido', 'desc');
}])->find($id);

I assume the relationship is called matriculas .

After the modification of the question the panorama changes, and I think the solution that best fits is to use the type of relation hasManyThrough to pass from subject to students through enrollments:

Subject Model:

public function alumnos()
{
    return $this->hasManyThrough(
        'App\Matricula', 'App\Alumno',
        'asignatura_id', 'matricula_id', 'id'
    )->orderBy('apellido_paterno', 'desc');
}

In the controller you can call it that, although there is NO need:

$alumnos = $asignatura->alumnos;

However to avoid that unnecessary line, I would simply iterate in the view in $asignatura->alumnos

    
answered by 20.06.2017 в 05:22