Retouch several items of a relation - Laravel

Asked

Viewed 281 times

0

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

I created a show function in the controller and wanted to make sure that when clicking on the details it showed all the students enrolled in a course. show in Cursocontroller

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 name of the course to the envés of the relation of students.

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);
        }
}

Anyone can help?

In the end it was solved like this: Cursocontroller

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');
  • You can create a course method within the Student Model, and vice versa. This way, each student will have ->courses as well as each course will have ->students.

2 answers

1

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

Model Courses, whereas there is a Student Tablecourses dealing with relationships:

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

This will make the Course has the object students.

Then, your controller 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 there’s something missing, if you want to post more details of your structure, can you help me suggest something.


Edit:

We will manually specify the connections.

Read the references to clarify the ideas.

Change in your Student Model:

REF: https://laravel.com/docs/5.6/eloquent-relationships#one-to-one

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

Change in your Model Courses:

REF: https://laravel.com/docs/5.6/eloquent-relationships#one-to-Many

public function alunos()
    {
        return $this->hasMany(\App\Aluno::class, 'curso_id', 'id');
    }
  • I showed above how are the two models of the relationship, could give an analysis for me?

  • I don’t have this table Alunocursos

  • @Lintonjunior, check my Dit and confirm if it worked

  • Hello, David. It worked in parts, I gave a read and managed to solve, I edit the first question showing the solution?

  • Great! If my answer helped, mark as useful :D

  • I don’t know if the ideal is to release a full reply or edit its contents.

Show 1 more comment

0


When you use:

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

You’re saying, look for courses with id = $id, and that have students (you’re not doing the relationship).

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

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

On the controller:

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

In the view you can access the course data as name and other information, as well as access the relationships directly from the view;

$course->students $course->teacher

you can also use in the controller the following code:

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

so you already have the relationship loaded before Blade.

  • This part of loading the controller helped me a lot

Browser other questions tagged

You are not signed in. Login or sign up in order to post.