Level 55
@jufrensius look at this one https://laravel.com/docs/8.x/queries#conditional-clauses
->when($request->bu, fn ($query) => $query->whereIn('e.bu_id', $request->bu))
// the same for other filters
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I'd like to search or filter talents based on selected dropdown. But, In this case several dropdown are not mandatory. How to query selected and unselected dropdown's value?
This is my route
Route::get('/hav-box', [TalentSearchController::class, 'searchTalent']);
This is my method to search or filtering
public function searchTalent(Request $request)
{
//code here...
$talents = DB::table('employee AS e')
->select('e.profile_pic', 'e.name as name', 'hav_history.hav_id')
->selectRaw('TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age')
->join('bu', 'bu.id', '=', 'e.bu_id')
->join('division', 'division.id', '=', 'e.div_id')
->join('department', 'department.id', '=', 'e.dept_id')
->join('section', 'section.id', '=', 'e.sect_id')
->join('position', 'position.id', '=', 'e.pos_id')
->join('layer', 'layer.id', '=', 'e.layer_id')
->join('hav_history', 'hav_history.nrk', '=', 'e.nrk')
->whereIn('e.bu_id', $request->bu)
->whereIn('e.div_id', $request->div)
->whereIn('e.dept_id', $request->dept)
->whereIn('e.sect_id', $request->sect)
->whereIn('e.pos_id', $request->pos)
->whereIn('e.layer_id', $request->layer)
->whereIn('hav_history.hav_id', $request->hav)
->whereYear('hav_history.year', date('Y', strtotime('-1 year')))
->orderBy('e.nrk', 'asc')
->get();
return view('pages.hav-box', compact('talents'));
}
And this is the blade for searching or filtering
<section>
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h4 class="card-title">Talent Search</h4>
</div>
<div class="card-content">
<div class="card-body">
<form action="/hav-box" method="get">
@csrf
<div class="row">
<div class="col-md-4">
<div class="form-group">
<label for="bu">Business Unit</label>
<select name="bu[]" class="select2-customize-result form-control" multiple="multiple" id="bu">
@foreach ($bu as $data)
<option value="{{ $data->id }}">{{ $data->name }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="div">Division</label>
<select name="div[]" class="select2-customize-result form-control" multiple="multiple" id="div"></select>
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<label for="dept">Department</label>
<select name="dept[]" class="select2-customize-result form-control" multiple="multiple" id="dept"></select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="sect">Section</label>
<select name="sect[]" class="select2-customize-result form-control" multiple="multiple" id="sect"></select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="pos">Position</label>
<select name="pos[]" class="select2-customize-result form-control" multiple="multiple" id="pos"></select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label for="layer">Layer</label>
<select name="layer[]" class="select2-customize-result form-control" multiple="multiple" id="layer">
@foreach($layer as $data)
<option value="{{$data->id}}">{{$data->name}}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label for="hav">HAV</label>
<select name="hav[]" class="select2-customize-result form-control" multiple="multiple" id="hav">
@foreach($hav as $data)
<option value="{{$data->id}}">{{$loop->iteration}} - {{$data->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
<div class="d-flex mt-2 flex-row-reverse">
<button type="submit" class="btn btn-primary">Generate</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</section>
@jufrensius look at this one https://laravel.com/docs/8.x/queries#conditional-clauses
->when($request->bu, fn ($query) => $query->whereIn('e.bu_id', $request->bu))
// the same for other filters
Please or to participate in this conversation.