Retrieving multiple items in a relationship - Laravel

0

I'm having a little problem, I have a course and student relationship in the system.

I created a show function in the controller and wanted to make clicking on the details show all students enrolled in a course. show on CourseController

public function mostrar($id)
        {

            $alunos = Curso::whereHas('alunos')->with('alunos')->where('id', $id)->get();


            return view('curso.mostrar', compact('alunos'));

        }

And my show.blade.php

<div class="container">
    <br>
    <div class="pull-right">
        <a class="btn btn-success" href="{{ action('CursoController@index') }}"> Voltar</a>
    </div>
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Nome</th>
        <th>Cidade</th>
        <th>Estado</th>
      </tr>
    </thead>
    <tbody>
      @foreach($alunos as $aluno)
      <tr>
        <td>{{$aluno->id}}</td>
        <td>{{$aluno->nome}}</td>
        <td>{{ $aluno->cidade }}</td>
        <td>{{ $aluno->estado}}</td>
      </tr>
      @endforeach
    </tbody>
  </table>
  </div>

The problem here is that it is returning the course name instead of the student list.

My course model is like this

class Curso extends Model
{
    protected $fillable = ['professor_id', 'nome'];

    public function professor()
    {
        return $this->belongsTo(\App\Professor::class);
    }

    public function alunos()
    {
        return $this->hasMany(\App\Aluno::class);
    }
}

My student model is like this

class Aluno extends Model
{
    protected $fillable = ['curso_id','nome','data_nascimento', 'cep', 'logradouro', 'numero', 'bairro', 'cidade', 'estado'];


    public function curso()
        {
            return $this->belongsTo(\App\Curso::class);
        }
}

Can anyone help?

In the end it was solved like this: CourseController

public function mostrar($id)
        {

            $curso = Curso::findOrFail($id);
            $alunos = $curso->alunos;
            return view('curso.mostrar', compact('curso', 'alunos'));

        }

And the route

Route::get('cursos/{id}/mostrar', 'CursoController@mostrar');
    
asked by anonymous 26.03.2018 / 20:36

2 answers

0

When you use:

  

$ students = Course :: whereHas ('students') -> with ('students') -> where ('id', $ id) - > get ();

You're saying, look for courses with id = $ id, and they have students (you're not making the relationship).

First we go to CourseController. You can pass by already the course parameter. on the route would be:

Route::get('/{curso}/mostrar', 'CursoController@mostrar');

No controller:

public function mostrar(Curso $curso){
    return view('curso.mostrar', compact('curso'));
}

In the view you can access the course data as a name and other information, as well as access the direct relationships of the view;

$ course-> students    $ course-> teacher

You can also use the following code in the controller:

public function mostrar(Curso $curso){
    $curso = $curso->load('alunos','professor');
    return view('curso.mostrar', compact('curso'));
}

So you already have the relationship loaded before the blade.

    
27.03.2018 / 03:13
1

This will depend a little on the relationship, but this can help you:

Model Courses, considering that there is a Student Alumni table dealing with relationships:

class Cursos extends
{
    public function alunos()
    {
        return $this->hasMany('AlunosCursos', 'cursos_id', 'id');
    }
}

This will cause the Course to have the students object.

So, your controler can simply pass the course normally, and in the view, you access the students.

Your Controller:

public function mostrar($id)
{
   $curso = Curso::with('alunos')->where('id', $id)->get();
   return view('curso.mostrar', compact('curso'));
}

In view:

{{ $curso->alunos }}

I'm sorry if something was missing, if you want to post more details of your structure, you can help me suggest something.

Edit:

Let's manually specify the connections.

Read the references to clear ideas.

Change in your Student Model:

REF: link

public function curso()
{
   return $this->hasOne(\App\Curso::class, 'id', 'curso_id');
}

Change in your Model Courses:

REF: link

public function alunos()
    {
        return $this->hasMany(\App\Aluno::class, 'curso_id', 'id');
    }
    
26.03.2018 / 20:49