I have a table that shows some data from the database in MYSQL which I have 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 record of my complex table , and hide the other columns of the table (in my view) where the records do not belong to that city.
So far I've tried this, which hides the columns for me.
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 have also tried with 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 don't know very well how I can put a foreach() so that it shows me all the results that match the city of the logged in user, with that of 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 try to validate that is in a similar way to the following query made in MySQL.
Also as additional information I add the relationships between the tables.
I don't fully understand what you want to do, but if I manage to understand the cities filter, what you should do is relate the users table to the cities table in this way, you will have the cityid in users and when you call the information in the for each you will compare that the other users have the same idcity as the logged in user, in this case you will compare which complex has the same idcity as your user, so you will also have to relate complex with city:
Seeing the DB, you could bring all the users that also share the city of the logged in in this way:
at the end with return you take the data where you want.
I answer my question, at the moment I managed to filter the fields of the table according to the city of the logged in user redirecting to a different view (When my idea was in the same view; but I had to do it like this since it was the way I found and it worked for me )
To achieve that I had to add a new field to my users table, the city.