How to filter columns from a table with checkbox in Laravel

4

I have a table that shows some data from the database in MYSQL which has a checkbox that acts as a filter which, should validate that the city of the user connected in the session is equal to the city of some registry of my table complexes , and hide the other columns of the table (in my view) where the records do not belong to that city.

At the moment I've tried this, which hides the columns.

window.onload = function(){

    $('#empty').hide();
}

$("#filtro_check").on("change", function(){

    var ciudad = document.getElementById('input_comuna').value;

    if ($(this).prop('checked')){
        if ($('#input_complejo_id_user').val() == $('#input_id_complejo').val()){


            $('td:nth-child(1)').toggle();
            $('td:nth-child(2)').toggle();
            $('td:nth-child(3)').toggle();
            $('td:nth-child(4)').toggle();
            $('td:nth-child(5)').toggle();
            $('td:nth-child(6)').toggle();
            $('td:nth-child(7)').toggle();
            $('#empty').show();


        }
    }else if ($(this).prop('checked') == false) {

        $('td:nth-child(1)').toggle();
        $('td:nth-child(2)').toggle();
        $('td:nth-child(3)').toggle();
        $('td:nth-child(4)').toggle();
        $('td:nth-child(5)').toggle();
        $('td:nth-child(6)').toggle();
        $('td:nth-child(7)').toggle();
        $('#empty').hide();

    }
});

And this is the table.

@foreach($complejos as $comp)
    @if(Auth::user()->complejo_id == $comp->id)

        #ESTE ES EL CHECKBOX
        <input type="checkbox" id="filtro_check" name="filtro_check">
        <label for="filtro_check">Filtrar por ciudad</label>


        <input type="hidden" value="{{Auth::user()->complejo_id}}" id="input_complejo_id_user">
        <input type="hidden" value="{{$comp->comuna}}" id="input_comuna">
        <input type="hidden" value="{{$comp->id}}" id="input_id_complejo">
    @endif
@endforeach


<table width="100%" border="0" id="tabla_principal">
    <thead >
        <tr>
            <th class="th">Complejo</th>
            <th class="th">Dirección</th>
            <th class="th">Teléfono</th>
            <th class="th">Detalle</th>
            <th class="th">Fecha</th>
            <th class="th">Seleccionar</th>
        </tr>
    </thead>

    <tbody>
        @foreach($complejos as $complejo)
            <form method="POST" action="{{route('reservar-cancha-horarios',['id'=>$complejo->id])}}" name="form" id="form">
                <tr>
                    <td>{{$complejo->nombre}}</td>
                    <td>{{$complejo->direccion}}</td>
                    <td>{{$complejo->telefono}}</td>

                    <td>
                      <input type="button" id="openModal">
                    </td>

                    <td style="padding-top: 5mm;">
                      <input type="date" name="fecha" required="required" id="fecha">
                    </td>

                    <td style="padding-top: 5mm;">
                      <input type="submit" name="guardar" id="btn_seleccionar" value="SELECCIONAR">

                    </td>
                    {!! Form::token() !!}
                </tr>
            </form>
        @endforeach
    </tbody>
</table>

<center>
    <label id="empty">Su busqueda no coincide con nuestros registros.</label>
</center>

I've also tried this:

public function filtroCiudad($id){

  $user_id = User::find($id);
  $user_logged_id = Auth::user()->id;
  $complejo = Complejo::find($id);

  try {

      /*Además de la condición actual, cómo en el IF puedo preguntar 
      si el valor del campo $complejo->comuna es igual en todos los registros de mi tabla?? 

      Si se cumple debería mostrar todos los campos donde coincida la comuna o ciudad*/

      if ($user_logged_id == $user_id->id) {

        return view('reservas-cancha')->with('user', $user_id)->with('complejo', $complejo);
      }
  } catch (\Illuminate\Database\QueryException $e) {

      Session::flash('error', 'Su búsqueda no coincide con nuestros registros');
      return view('reservas-cancha')->with('user', $user_id)->with('complejo', $complejo);  
  }
}

And in my view I have the following table:

@if(Session::has('error'))
    <span class="error">{{Session::pull('error') }}</span>
@endif

<table width="100%" border="0" id="tabla_principal">
    <thead >
        <tr>
            <th class="th">Complejo</th>
            <th class="th">Dirección</th>
            <th class="th">Ciudad/Comuna</th>
            <th class="th">Detalle</th>
            <th class="th">Fecha</th>
            <th class="th">Seleccionar</th>
        </tr>
    </thead>

    <tbody>
    #O POSIBLEMENTE COLOCAR TAMBIÉN AQUÍ UN @FOREACH
        <tr>
            <td>{{$complejo->nombre}}</td>
            <td>{{$complejo->direccion}}</td>
            <td>{{$complejo->comuna}}</td>

            <td>                             
                <input type="button" class="detalle" id="btn_select" data-target="#myModal" value="DETALLE" data-toggle="myModal">
            </td>

            <td>
                <input type="date" name="fecha" class="date_control" required="required" id="fecha">
            </td>

            <td>
                <input type="submit" name="guardar" class="btn btn-danger" id="btn_seleccionar" value="SELECCIONAR">
            </td>
        </tr>
    </tbody>
</table>

Although I do not know very well in what way I can put a foreach () to show me all the results that match the city of the user logged in, with the database, of my table of complexes. Since I have many records with the same city and different IDs each.

An example of how I intend to validate that is in a similar way to the following MySQL query.

Also as additional information I add the relationships between the tables.

    
asked by M4uriXD 24.12.2018 в 14:41
source

3 answers

0

I answer my question, for now I managed to filter the fields of the table according to the city of the user logged in, redirecting to a different view (When my idea was in the same view, but I had to do it like this already that was the way I found it and it worked for me.

To achieve that I had to add a new field to my users table, the city.

  • The first thing that was done was to change the checkbox by a button.
<div class="div_filtro">
    @foreach($complejos as $comp)
        @if(Auth::user()->complejo_id == $comp->id)

            <a href="{{route('filtro.ciudad', Auth::user()->id)}}">
                <input class="btn_ciudad" value="Filtrar por ciudad" type="button">
            </a>
        @endif
    @endforeach
</div>
  • Then the route to be occupied was created.
Route::get('reservar', ['uses' => '[email protected]', 'as'=>'filtro.ciudad']);
  • Then the function that the route occupies was created in the controller.
public function filtroCiudad(){


  $user_id = User::all();
  $user_logged_id = Auth::user()->id;
  $complejo = Complejo::all();

  try {
    foreach ($user_id as $user_id ) {

      if ($user_logged_id == $user_id->id) {

        return view('reservas-cancha')->with('user', $user_id)->with('complejo', $complejo);
      }
    }
  } catch (\Illuminate\Database\QueryException $e) {

    Session::flash('error', 'Su búsqueda no coincide con nuestros registros');
    return view('reservas-cancha')->with('user', $user_id)->with('complejo', $complejo);  
  }
}
  • And finally in the new view the corresponding fields are shown.
/*TODO ESTO ESTÁ DENTRO DE UNA TABLA (<table>) ya que al 
ser muy extenso el código solo copié la parte importante*/

@foreach($complejo as $complejo)
    @if($user->city == $complejo->comuna)
        <form method="POST" action="{{route('reservar-cancha-horarios',['id'=>$complejo->id])}}" name="form" id="form">
            <tbody>
                <tr>
                    <td id="td_1">{{$complejo->nombre}}</td>
                    <td id="td_2">{{$complejo->direccion}}</td>
                    <td id="td_3">{{$complejo->telefono}}</td>                                 
                    <td>                                             
                        <input type="button" class="detalle" id="btn_select" data-target="#myModal" value="DETALLE" data-toggle="myModal" data-id="{{$complejo->id}}" data-nombre="{{$complejo->nombre}}" data-direccion="{{$complejo->direccion}}" data-telefono="{{$complejo->telefono}}" data-comuna="{{$complejo->comuna}}" data-descripcion="{{$complejo->descripcion}}">
                    </td>

                    <td>
                        <input type="date" name="fecha" class="date_control" required="required" id="fecha">
                    </td>

                    <td>

                        <input type="submit" name="guardar" class="btn btn-danger" id="btn_seleccionar" value="SELECCIONAR">
                    </td>
                {!! Form::token() !!}
                </tr>
            </tbody>
        </form>
    @endif
@endforeach
    
answered by 31.12.2018 / 16:02
source
0

I do not fully understand what you want to do, but if I can understand the cities filter, what you should do is relate the user table with the city table in this way, you will have the user id city and when you call the information in the for each you compare that the other users have the same idciudad of the logged in user, in this case you will compare which complex has the same id city of your user then you will also have to relate complex with city:

 @foreach($complejos as $complejo)
        <tr>
            @if(Auth::user()->idciudad == $complejo->idciudad)
            <td>{{$complejo->nombre}}</td>
            <td>{{$complejo->direccion}}</td>
            <td>{{$complejo->comuna}}</td>              
          @endif
        </tr>
 @endforeach
    
answered by 26.12.2018 в 18:10
0

Viewing the BD, you could bring all the users that also share the city of the logeado in this way:

/* importaciones laravel en controlador  */

use Illuminate\Support\Facades\Auth;

use DB;

/* Y esto deberia ir dentro del metodo del controlador    */

$user = Auth::user();

$complejoId = $user->complejo_id;

/* Se realiza consulta   */

$data = DB::select('SELECT u.name, u.lastname FROM users as u INNER JOIN complejo AS c ON u.complejo_id=c.id WHERE c.id = ?', array($complejoId));

at the end with return you carry the data wherever you want.

    
answered by 28.12.2018 в 21:57