How I can short my code when request have many parameters Brief:

I've create simple filter in my laravel project. I'll send parameters with ajax request.

Code:

public function specialitiesAjaxLoadMore(Request $request)
{
    $city = null;
    if(isset($request-&gt;city_id)) {
        $city = City::find($request-&gt;city_id);
        if($city !== null) {
            $city = $city-&gt;id;
        }
    }

    $sex = null;
    if((isset($request-&gt;sex) &amp;&amp; is_numeric($request-&gt;sex)) &amp;&amp; $request-&gt;sex == 1 || $request-&gt;sex == 2) {
        $sex = $request-&gt;sex;
    }

    $ageFrom = null;
    if((isset($request-&gt;ageFrom) &amp;&amp; is_numeric($request-&gt;ageFrom)) &amp;&amp; $request-&gt;ageFrom &gt; 7) {
        $ageFrom = $request-&gt;ageFrom;
    }

    $ageTo = null;
    if((isset($request-&gt;ageTo) &amp;&amp; is_numeric($request-&gt;ageTo)) &amp;&amp; $request-&gt;ageTo &gt; 7) {
        $ageTo = $request-&gt;ageTo;
    }

    if(isset($city)) {
        $candidates = Candidate::where('city_id', $city)-&gt;get();
    }

    if(isset($sex)) {
        $candidates = Candidate::where('sex', $sex)-&gt;get();
    }

    if(isset($ageFrom)) {
        $candidates = Candidate::where('birthday', '&lt;=', date('Y-m-d', strtotime("-$ageFrom years")))-&gt;get();
    }

    if(isset($ageTo)) {
        $candidates = Candidate::where('birthday', '&gt;=', date('Y-m-d', strtotime("-$ageTo years")))-&gt;get();
    }

    if(isset($city, $sex)) {
        $candidates = Candidate::where('city_id', $city)-&gt;where('sex', $sex)-&gt;get();
    }

    if(isset($city, $sex, $ageFrom)) {
        $candidates = Candidate::where('city_id', $city)
                               -&gt;where('sex', $sex)
                               -&gt;where('birthday', '&lt;=', date('Y-m-d', strtotime("-$ageFrom years")))
                               -&gt;get();
    }

    if(isset($city, $sex, $ageFrom, $ageTo) &amp;&amp; $ageTo &lt; $ageFrom) {
        $candidates = Candidate::where('city_id', $city)
                               -&gt;where('sex', $sex)
                               -&gt;where('birthday', '&lt;=', date('Y-m-d', strtotime("-$ageFrom years")))
                               -&gt;where('birthday', '&gt;=', date('Y-m-d', strtotime("-$ageTo years")))
                               -&gt;get();
    }

}


In my case there will be a lot of parameters from the request and if I continue to write everything in such a way as seisai then there will be a lot of combinations. So is it possible to improve or shorten my current code?
https://laravel.com/docs/5.7/queries#where-clauses

$where = [];
if($city) {
    $where[] = ['city_id', '=', $city];
}
if($sex) {
    $where[] = ['sex', '=', $sex];
}
if($ageFrom) {
    $where[] = ['birthday', '&lt;=', date('Y-m-d', strtotime("-$ageFrom years"))];
}
if($ageTo) {
    $where[] = ['birthday', '&gt;=', date('Y-m-d', strtotime("-$ageTo years"))];
}
$candidates = Candidate::where($where)-&gt;get();