Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

omontes's avatar

How to create a dynamic query using eloquent

How to create a dynamic query using eloquent:

sending an array of checkbox and need to use it in the where

//form
@foreach($tipos as $tipo)
<input type="checkbox" name="estaciones[]" value="{{ $tipo->id }}" id="{{ $tipo->id }}">{{ $tipo->nombre }} <br>
@endforeach
Route::get('getEstaciones', function(){

$opciones = Input::get('info');

$list = Station::where('tipoestacion_id', '=', )->get();

return json_encode($list);
});
0 likes
3 replies
pmall's avatar

Query methods return a query object that is chainable.

$query = Station::select();

foreach($conditions as $column => $value)
{
  $query->where($column, '=', $value);
}

$stationes = $query->get();

Note that you can use orWhere for a or clause. Note also that you can group where/orWhere clauses using a callback in a where/orWhere clause. Check the docs about that.

5 likes
bestmomo's avatar

Or maybe :

$list = Station::whereIn('tipoestacion_id', Input::get('estaciones'))->get();

Please or to participate in this conversation.