I have a table with a series of fields in a view, and I would like to filter those fields (within the same view) in different tables.
This view fetches some fields from the database, via an SQL query with Laravel's Eloquent in the controller, in a variable named $horarios
:
This is the controller that receives the data from a first view
public function horarios($id,Request $request){
$fecha = date('Y-m-d', strtotime($request->input('fecha')));
$horarios = HorariosNew::join('cancha','cancha.id','=','horario.cancha_id')
->leftJoin('reserva','reserva.horario_id','=','horario.id')
->where('horario.hora_inicio','>',date('Y-m-d H:m:s'))
->where('horario.hora_inicio','LIKE',$fecha.'%')
->where('cancha.complejo_id',$id)
->groupBy('horario.id','horario.hora_inicio','reserva.horario_id','cancha.nombre','cancha.deporte','reserva.estado_reserva_id','reserva.horario_id')
->orderBy('cancha.nombre','asc')
->orderBy('horario.hora_inicio','asc')
->get(['horario.id','horario.hora_inicio','cancha.nombre as cancha','cancha.deporte','reserva.estado_reserva_id','reserva.horario_id']);
//Y REDIRECCIONA A LA VISTA EN DONDE TENGO LA TABLA
return view('reservar-cancha-horarios',['complejo'=>$id,'horarios'=> $horarios]);
}
Below I leave an example of how the data appears, which are all together, and it is where I look for that same thing to appear in different tables.
The field to filter is the name of the court. Which I want to be displayed in different tables within the view along with its corresponding data.
So far what I was trying was the following:
In the following lines of my table in the view:
<table>
...
@elseif($horario->cancha = 'Cancha 1')
<form method="POST" action="{{route('reservar-confirmar',['complejo'=>$complejo])}}">
<td>{{$horario->hora_inicio}}</td>
<td>{{$horario->cancha}}</td>
<td>{{$horario->deporte}}</td>
<td class="text-primary">Disponible</td>
{!! Form::token() !!}
<td>
<input type="submit" name="guardar" class="btn btn-danger" id="btn-seleccionar" value="Seleccionar">
</td>
</form>
@endif
...
</table>
I managed to show only court 1 in the table.
To do the same with the others, he only had to repeat the complete structure of the table, modifying what was necessary.
Although I know that this will fail if that field that it brings from the database changes to a different value, being that the case it would show me the empty table.
Therefore, I don't know very well how to compare it with itself, so that it is always the same value and consequently shows the corresponding fields in different tables.
You can do it with groupBy() . You only have to pass the name of ONE column, in this way it will group it correctly. Your controller should look like this:
public function schedules($id,Request $request){
}
Important: You must remove the groupBy() from your query and leave only the one I added. You can also do it directly in the query if you want, but I think it's more readable this way. On the other hand, I would review the issue of orderBy(), it seems strange to me since, as I understand it, there is no such method for eloquent. Cheers!
Answering my question I managed to solve the dilemma.
What I did was the following:
In my controller that received the data from a previous view add one more field, in
group by
it and then assign it as if it were a variable.I added the id of the court from my schedule table, and called it id_court.
Then in the view I only have to validate that with a sentence
IF-ElSE
Then all that remained was to repeat the structure of the complete table and show a different field, modifying the following line, for the field that you want to show, managing to separate the data, which it showed, into different tables: