3
\$\begingroup\$

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);
        });
    }
}
\$\endgroup\$
4
  • \$\begingroup\$ Are those sections mutually exclusive or can a request have multiple of those? \$\endgroup\$ Commented May 13, 2020 at 13:19
  • \$\begingroup\$ @Mast they are exclusive. \$\endgroup\$ Commented May 13, 2020 at 13:22
  • 1
    \$\begingroup\$ Then shouldn't you be using if...else if ? \$\endgroup\$ Commented May 13, 2020 at 13:23
  • \$\begingroup\$ @MD.TabishMahfuz At least, with a bit of magic a switch could work too, or a foreach over an array with the supported content. \$\endgroup\$ Commented May 13, 2020 at 14:42

1 Answer 1

2
\$\begingroup\$

You could try something like this:

private function applyFiltering($query, $request)
{

    $query->when($request->has('active_group'), function($query) use($request) {
        $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]);
        });
    })->when($request->has('waiting_group'), function($query) use ($request) {
        $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);
         });
    })
    ........
    ........
    ........
}
```
\$\endgroup\$
2
  • \$\begingroup\$ While this technically removes the if statements, I'm not sure this looks any better than the original. \$\endgroup\$ Commented May 13, 2020 at 22:07
  • \$\begingroup\$ Any callable should be able to be passed into Builder::when() so the filters could be moved to another class / separate private functions. \$\endgroup\$ Commented Jun 17, 2020 at 21:22

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.