This is pretty straightforward, it takes requests from the frontend, checking if it is present on the request, if it does, an action is taken for that if statement.
I wonder if there is a way to remove some if statements or make it less complicated.
private function applyFiltering($query, $request)
{
if ($request->has('active_group')) {
$query->whereNotIn('status_id', [1, 6, 4]);
$query->whereHas('userStatus', function ($query) use ($request) {
$query->where('user_id', $request->user()->id)->whereNotIn('status_id', [1, 6, 4]);
});
}
if ($request->has('waiting_group')) {
$query->whereNotIn('status_id', [1, 6]);
$query->whereHas('userStatus', function ($query) use ($request) {
$query->where('user_id', $request->user()->id)->whereNotIn('status_id', [1, 6])->where('status_id',4);
});
}
if ($request->has('meeting_group')) {
$query->where('type_id', 2);
}
if ($request->has('comp_meeting_group')) {
$query->where('type_id', 4);
}
if ($request->has('completed_group')) {
$query->whereHas('userStatus', function ($query) use ($request) {
$query->where('user_id', $request->user()->id)->where('status_id', 1);
});
}
if ($request->has('canceled_group')) {
$query->whereHas('userStatus', function ($query) use ($request) {
$query->where('user_id', $request->user()->id)->where('status_id', 6);
});
}
if ($request->has('shift_group')) {
$query->where('status_id', 1)->where('type_id', 6);
}
if ($request->has('announce_group')) {
$query->where('type_id', 3);
}
if ($request->has('task_id')) {
$query->where('id', 'like', '%' . $request->input('task_id') . '%');
}
if ($request->has('task_statuses')) {
$statuses = $request->input('task_statuses');
$query->whereIn('status_id', $statuses);
}
if ($request->has('task_types')) {
$statuses = $request->input('task_types');
$query->whereIn('type_id', $statuses);
}
if ($request->has('task_priorities')) {
$statuses = $request->input('task_priorities');
$query->whereIn('priority_id', $statuses);
}
if ($request->has('task_invite_users')) {
$toUsers = $request->input('task_invite_users');
$query->whereHas('taskAssignment', function ($query) use ($request, $toUsers) {
$query->whereIn('assigned_to', $toUsers);
});
// $query->whereIn('to_user_id',$statuses);
}
if ($request->has('task_users')) {
$byUsers = $request->input('task_users');
$query->whereHas('taskAssignment', function ($query) use ($request, $byUsers) {
$query->whereIn('assigned_by', $byUsers);
});
}
}
if...else if
? \$\endgroup\$