good, I have a datatable which I get the data by ajax, I manage to get the data from my timetable correctly, but it brings me the teacher and vehicle id, since these are the ones in the table, but I'm trying to get the name and surname of the teacher by means of the id and the same with the vehicle.
This is my datatable
var clase = "clase";
var fecha = "fecha";
var hora = "hora";
var minutos_bloque = "minutos_bloque";
var id_profesor = "id_profesor";
var id_vehiculo = "id_vehiculo";
var id_alumno_editar = $('#id_alumno').val();
var table3 = $('#horarioPracticoEditar_dt').DataTable({ // Propiedades de la Datatable Principal
'serverSide' : true,
'ajax' : {
'url': "{{ route('alumno.editar_horario') }}",
'data': {"id_alumno": id_alumno_editar},
'type': "GET"
},
'columns' : [
{render:
function ( data, type, row ) {
return 'Clase '+row[clase];
}
},
{data: fecha},
{data: hora},
{data: minutos_bloque},
{data: id_profesor},
{data: id_vehiculo},
{data: 'action', name: 'action', orderable: false, searchable: false}
]
});
This is the function with which I bring the data, what is commented is how I was trying to call the data, which according to me, I can manipulate in jquery to show it in the datatable (perhaps I am wrong here)
public function editar_horario(Request $request){ // TABLA HORARIO EDITAR
if ($request->ajax()) {
$id_alumno = $request->get('id_alumno');
$horario = horarioModel::latest()->where('id_alumno','=',$id_alumno)->get();
//$horario->profesor['nombre'];
//$horario->profesor->apellidos;
//$horario->vehiculo->marca;
//$horario->vehiculo->modelo;
//$horario->vehiculo->patente;
return datatables::of($horario)
->addIndexColumn()
->addColumn('action', function($row){
// EL ROW DEBE INDICAR EL NOMBRE DE LA ID EN LA TABLA
$btn = '<div class="btn-group elevation-2">
<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id_horario.'" data-original-title="Editar" class="btn btn-sm btn-outline-info editarAlumno"><i class="fas ion-ios-create" style="width: 20px;"></i></a>
<a href="javascript:void(0)" data-toggle="tooltip" data-id="'.$row->id_horario.'" data-original-title="Eliminar" class="btn btn-sm btn-outline-danger eliminarAlumno"><i class="fas ion-md-trash" style="width: 20px;"></i></a>
</div>';
return $btn;
})
->rawColumns(['action'])
->make(true);
}
return view('Alumno.alumno');
}
This is the way I have my relations in the timetable model, most likely it is here that I have the problem, but according to me it is fine, or it may be the way I am calling the relations in the controller
public function profesor(){
return $this->belongsTo(profesorModel::class, 'id_profesor', 'id_horario');
}
public function vehiculo(){
return $this->belongsTo(vehiculoModel::class, 'id_vehiculo', 'id_horario');
}
this is the teacher relationship
public function horario(){
return $this->hasMany(horarioModel::class, 'id_horario', 'id_profesor');
}
and this is my vehicle model
public function horario(){
return $this->hasMany(horarioModel::class, 'id_horario', 'id_vehiculo');
}
and for better understanding, this is what my datatable brings
SOLVED
I had a bad relationship with the FK
time model
teacher model
vehicle model
and in this way I sent it to my sight
in my jquery
and this would be the desired result