I have 4 tables that relate to each other in my database in PhpMyAdmin; I'm doing a php site with Laravel with Blade. And it turns out that I want to show some fields that in themselves have no relation with a main table that I have, but with others that are related in turn to the main table.
A concrete example would be the following.
I have a main view that brings me some data from my table reservas
which I do join
with other tables I leave the code from my controller where I do everything.
public function index(){
$usuario=Auth::user()->id;
$reservas=Reserva::join('horario','horario.id','=','reserva.horario_id')
->join('cancha','cancha.id','=','horario.cancha_id')
->join('complejo','complejo.id','=','cancha.complejo_id')
->join('estado_reserva','estado_reserva.id','=','reserva.estado_reserva_id')
->where('horario.hora_inicio','>=',date('Y-m-d'))
->where('complejo.id',Auth::user()->complejo_id)
->orderBy('horario.hora_inicio','asc')
->get(['reserva.id','complejo.nombre as complejo','cancha.nombre as cancha','cancha.deporte','horario.hora_inicio','estado_reserva.estado as estado']);
return view('operador-reservas',['reservas'=> $reservas]);
}
Then in my view I have a button that calls the function editar
of my controller. Which the code is as follows.
EL BOTÓN
<a href="{{url('operador_reservas/'.$reserva->id.'/edit') }}">
<input type="button" id="editar" value="MODIFICAR">
</a>
My controller:
public function edit($id){
$reserva = ReservasNew::find($id);
$horario = HorariosNew::all();
$complejo = ComplejosNew::find($id);
$canchas = CanchasComplejo::find($id);
return view('operador-reservas-modificar')->with('reserva', $reserva)->with('horario',$horario)->with('complejo', $complejo)->with('canchas', $canchas);
}
And this is the field which; in the new view that returns the function edit
, I want to show the name of the sports complex.
@if($complejo->cancha_id == $canchas->id)
<input type="text" value="{{$complejo->nombre}}">
@endif
I explain the relationship of the tables.
The complejos_deportivos
table is related to the canchas
table, then this canchas
table is related to the horarios
table and last the horarios
is related to the reservations table.
This way.
- reservations - > schedules
- schedules - > courts
- courts - > sports complexes
And the error I get is the following.
What could I have done wrong in my logic?