Laravel index blade show name of a foreign key

0

I would like to know how I could show the name of the specialty assigned to each doctor. This is my current doctor blade index:

@extends('layouts.app')
@section('content')
@include('layouts.menu')

<a href="createDoctor" class="btn btn-info">Registrar un nuevo médico</a>
<table class="table table-striped">
 <thead>
<th>ID</th>
<th>Nombre</th>
<th>Apellidos</th>
<th>Email</th>
<th>Consulta</th>
<th>ID especialidad</th>
<th>Número de teléfono</th>
<th>Género</th>
<th>Administrador</th>
<th>Edición</th>
<th>Borrar médico</th>
  </thead>


   </tbody>
  @foreach ($doctors as $doctor) 

<tr>
<td>{{ $doctor->id}}</td>
<td>{{ $doctor->name}}</td>
<td>{{ $doctor->surnames}}</td>
<td>{{ $doctor->email}}</td>
<td>{{ $doctor->room}}</td>

<td>{{ $doctor->speciality_id}}</td>

<td>{{ $doctor->phone}}</td>
<td>{{ $doctor->gender}}</td>
<td>{{ $doctor->admin}}</td>
<td>
  <a href="editDoctor?id={{ $doctor->id}}" class="btn btn-info">Editar médico</a>
   </td>
    <td>
     <a href="deleteDoctor?id={{ $doctor->id}}" class="btn btn-danger">Eliminar</a>

     </td>

      </tr>

     @endforeach
     </tbody>
     </table>



          @endsection

And the index method of your controller:

public function index()
{
    //
    $specialities = Speciality::all();
    $doctors = DB::table('users')
      ->join('doctors', 'users.id','doctors.user_id')
      ->select('users.*','doctors.*')
      ->get();


    return view('doctors/index')->with('doctors',$doctors)->with('specialities',$specialities);

       }

Then I tried adding this to the index blade:

@foreach($doctors as $doctor)
@foreach ($specialities as $speciality)
<tr>
<td> {{$doctor->id}}</td>
 ......

With this I managed to show the name of the specialty but not in the way expected. Then I tried to write simply <td>{{doctor->speciality->name}}</td> but it did not work.

    
asked by Montu 30.05.2018 в 21:37
source

2 answers

1

If you are going to do it that way then you should print the specialty when it belongs to the respective doctor, this you do comparing the id of the doctor with the id_doctor that you should have in the table specialties:

@foreach($doctors as $doctor)
<tr>
<td>
    @foreach ($specialities as $speciality)
        @if($doctor->id == $speciality->doctor_id)
            {{ $speciality->name }}
        @endif
    @endforeach
</td>
 ......

This way you ensure that the specialty that corresponds to each doctor is printed.

Another way in which it could be done is from the same query to bring the specialty of each doctor by means of a join , thus avoiding having to load all the specialties and then having to go through them:

public function index()
{
    $doctors = DB::table('users')
      ->join('doctors', 'users.id','doctors.user_id')
      ->join('specialitys', 'users.id','specialitys.doctor_id')
      ->select('users.*','doctors.*','specialitys.name as speciality')
      ->get();

    return view('doctors/index')->with('doctors',$doctors);

}

To then show in the view as follows:

@foreach($doctors as $doctor)
<tr>
<td>
    {{ $doctor->speciality }}
</td>
 ......
    
answered by 30.05.2018 / 21:43
source
0

With Eloquent you can achieve this by using a belongsTo relationship. I suggest the following:

Create a model Doctor (if you do not have one), in which you will have a method like this:

public function speciality()
{
    return $this->belongsTo('App\Speciality', 'speciality_id', 'id');
}

Then modify your query in your controller:

$doctors = DB::table('users')
      ->join('doctors', 'users.id','doctors.user_id')
      ->select('users.*','doctors.*')
      ->get();

By:

$doctors = App\Doctor::join('doctors', 'users.id','doctors.user_id')
                 ->select('users.*','doctors.*')
                 ->get();

And in your view, you go through the doctor's collection (as you do it), and you only need to access the specialty as you wish:

{{ $doctor->speciality->name }}

If you need more information on how to use relationships with Eloquent, you can follow the official Laravel documentation: link

    
answered by 30.05.2018 в 22:04