Well, I have the following problem, I really hope you can help me.
I'm doing a query with Laravel's queryBuilder, the problem is that I have fields from different tables (Patient and Doctor) with the same attribute name and the data overlaps and I don't know what to do.
This is the query
$MisPacientesHoy = DB::table('fichamedica')
->join('Medicos','fichamedica.Medico_ID','=','Medicos.ID')
->join('Pacientes','fichamedica.Paciente_ID','=','Pacientes.ID')
->select('Pacientes.ID','Pacientes.nombre','Pacientes.apellidos','Pacientes.carnet','Pacientes.fechanacimiento','Pacientes.telefono','fichamedica.estado')
->where('fichamedica.Medico_ID', '=',$Medico_ID)
->where('fichamedica.fecha','=',$fecha_actual)
->where('fichamedica.turno','=',$Turno)
->orderBy('fichamedica.NroFicha', 'asc')
->get();
The problem is that I need this query to also return my doctor's data such as name, surname, and ID, but since the patient already has the property or attribute called 'name' when doing it @foreach
in my view, the data they will overlap
@foreach ($MisPacientesHoy as $Paciente )
<div>{{ $Paciente->nombre." ".$Paciente->apellidos." ".$Paciente->Medicos->nombre }}</div>
As I said, the query is fine, but I don't know how to return data from different tables with the same attribute in the same query.
Well, the solution was as simple as using a
alias
for the name of my fields.It's simple. When making a query,
select
you assign an alias to the field that generates a conflictAnd already in the view, you use
alias
it as if it were another normal field.