How to only query category if post value exists

Not to familiar with doing if statement where quires are concerned so looking for some help/advice.

Below is what i have tried (novice alert)


        $query = Reviews::find()

        ->andFilterWhere(['like', 'name', $_POST['Product']['name']])

        if(!empty($_POST['Product']['name'])){

            ->andFilterWhere(['in', 'brand_id', $_POST['Product']['brand_id']])

        }

        if(!empty($_POST['Product']['name'])){

        ->andFilterWhere(['in', 'category_id', $_POST['Product']['category_id']])

        };

Should be:




        $query = Reviews::find()

        ->andFilterWhere(['like', 'name', $_POST['Product']['name']]);


        if(!empty($_POST['Product']['brand_id'])){

            $query->andFilterWhere(['in', 'brand_id', $_POST['Product']['brand_id']])

        }


        if(!empty($_POST['Product']['category_id'])){

            $query->andFilterWhere(['in', 'category_id', $_POST['Product']['category_id']])

        };