Relations in laravel

1

Good, I have a view where I receive a "subject id", "student id" and I want to show the qualifications that ESE student has in ESA ASIGNATURA.

I have the following function

 public function verCalificacion($id, $idasi)
{
    $alumno = Alumno::find($id);
    $asignatura = Asignatura::find($idasi); 
    $mis_notas = $alumno->calificaciones; // ????

    return view('datos-profesor.vercalificacion')->with('alumno',$alumno)->with('mis_notas',$mis_notas)->with('asignatura',$asignatura);
}

where I receive the corresponding ids, but I do not know how to use the relationship to return the grades of a student in a subject, in the table Qualifications of the database I have the corresponding foreign ids and these are the relations

Rating

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


public function asignatura()
{
    return $this->belongsTo(Asignatura::class, 'id_asignatura', 'id');
}

Student

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

Subject

public function calificaciones()
{
    return $this->hasMany('App\Calificacion','id_asignatura');
}

Try a couple of wheres but it does not work for me, who can help me please. Thanks in advance.

    
asked by Edgardo Escobar 11.03.2017 в 05:21
source

2 answers

1

As I can see the relationships are fine in the models then how are you working with ORM you do not need $asignatura = Asignatura::find($idasi);

In your view you can directly access it from the variable $alumno

Example:

@foreach($alumno->calificaciones as $calificacion)
     {{ $calificacion->asignatura->nombre_asignatura }}
@foreach
    
answered by 14.03.2017 в 02:01
0

You can do something like this.

$alumno = DB::table('alumnos as a')
            ->join('calificaciones as cal', 'a.atributo_llaveforanea', '=', 'cal.llave_foranea')
            ->orderBy('idalumno', 'desc')
            ->get();
        return view('datos-profesor.vercalificacion', ["calificaciones" => $alumno]);

Where a.atributo_llaveforanea and a.atributo_llaveforanea are the related attributes.

Do not forget to include the class use DB; at the start of your controller

    
answered by 11.03.2017 в 06:47