Error with table relationships and showing a value in Laravel

0

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?

    
asked by M4uriXD 30.11.2018 в 21:19
source

3 answers

0

SOLVED, I post the solution to my question in case someone else serves him.

On the controller:

public function edit($id){

    $reserva = ReservasNew::find($id);
    $horario = HorariosNew::all();

    //Se hace el llamado a la otra tabla mediante join()

    $complejo = DB::table('cancha')
        ->join('complejo', 'complejo.id','=', 'cancha.complejo_id')->get();

    $cancha = DB::table('complejo')
        ->join('cancha', 'cancha.complejo_id', '=', 'cancha.id')->get();

    return view('operador-reservas-modificar')->with(['reserva'=>$reserva,'horario'=>$horario,'complejo'=>$complejo, 'cancha'=>$cancha]);
}

Now in the view

<!--DEJAMOS ESTOS INPUTs ESCONDIDOS
    #
    PORQUE ASÍ SE PUEDE MOSTRAR BIEN EL NOMBRE DEL COMPLEJO DEPORTIVO Y DE LA CANCHA-->

    @foreach($complejo as $complejo)
        @if($complejo->id == $complejo->complejo_id)
            <input type="hidden" value="{{$complejo->nombre}}">   
        @endif
    @endforeach

    @foreach($cancha as $cancha)
        @if($cancha->complejo_id == $cancha->id)
            <input type="hidden" value="{{$complejo->nombre}}">   
        @endif
    @endforeach
<!---->

<!--Y a continuación viene el verdadero campo, el input en su propiedad value="" se 
    debe llamar a la variable definida anteriormente en el controlador-->

<label class="text-label">Complejo</label>
<br>
<input type="text" value="{{$complejo->nombre}}" readonly="readonly" id="complejo">

<!--Se repiten estas 2 ultimas lineas para tambien agregar la otra variable definida 
    ($cancha) asignando su valor correspondiente en la propiedad value del input-->

Also, I leave the documentation where everything is explained in a better way: link

    
answered by 03.12.2018 / 16:19
source
0

You must add a condition in your foreach for the case in which some variable is empty, it can be like this:

@if(isset($complejo) && isset($canchas))
  @foreach($complejo as $complejo)
    @foreach($canchas as $canchas)
        @if($complejo->cancha_id == $canchas->id)
            <input type="text" value="{{$complejo->nombre}}">   
        @endif
    @endforeach
  @endforeach
@endif
    
answered by 30.11.2018 в 21:54
0

When calling the view from the controller, modify your syntax, like this:

return view('operador-reservas-modificar')
    ->with([
        'reserva'=>$reserva,
        'horario'=>$horario,
        'complejo'=>$complejo,
        'canchas'=>$canchas
    ]);

Do not ask me, why, but one of the classmates worked well when the other did not work for him. I always use this and I enjoy all the data. As the exception throws it at your sight, it gives me that some data is not happening to you.

I hope it helps you.

    
answered by 30.11.2018 в 23:58