relations in laravel 3 tables

-1

I have 3 tables:

Student (id)

Course (id, name)

Enrollment (id, student_id, course_id)

When I login with a student in his account I receive his data with:

$alumno = Alumno::find(auth('alumno')->user()->id);

I tried to get to the registration with:

$matricula = Matricula::where('id_alumno',auth('alumno')->user()->id)->get();

In the enrollment model I already have the relationship for course but doing this:

$matricula->curso;

But the relationship does not work, How can I get the name of the student's course by means of the registration?

Registration Model:

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

Course Model:

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

Student Model

public function matriculas()
{
    return $this->hasMany('App\Matricula','id_alumno');
}
    
asked by Edgardo Escobar 19.06.2017 в 08:00
source

1 answer

0

I in your case would do something similar to this

$alumno = Auth::user()->id

Since your Student table only has one field, with this you would already get the required id.

Now for enrollment:

$matricula=Matricula::where('id_alumno','=',$alumno)->get()->first();

This does not return an object, so you will not be able to apply $ tuition-> course. The get () return is a collection.

I hope you serve

    
answered by 19.06.2017 / 09:57
source