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.