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.