Skip to content
Advertisement

How do where one to three values ​come from the combo box in Eloquent Laravel?

I have a filter with three check boxes, each of which has a Value “question”, “partnership”, “complaint”. but when I select two data from the check box (ex.complaint & question) sent to the controller and I use the orWhere function, the data is error (all appearing). Is what I’m doing wrong?

$data = BrandEnquiry::whereHas('brand', function($query) use ($post){
                $query->where('id_brand', '=' , $post['id_brand']);
            });

            if(isset($post['question'])){
                $data->where(function ($key) use ($post){
                    $key->orWhere('subject', 'LIKE', '%'.$post['question']);
                });
            }

              if(isset($post['complaint'])){
                $data->where(function($q)use($post){
                        $q->orWhere('subject', 'LIKE', '%'.$post['complaint']);

                    });

            }


            if(isset($post['partnership'])){
                $data->where(function($q)use($post){
                        $q->orWhere('subject', 'LIKE', '%'.$post['partnership']);

                    });

            }




            $data = $data->orderBy('updated_at','desc')
            ->with('brandOutlet')
            ->with('user')
            ->get();

and HTML

<div class="row">
                <div class="col-lg-8 chxBox">
                    <label>Subject:</label>


                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="question" id="question" name="question">
                        <label class="form-check-label" for="defaultCheck1">
                        Question
                        </label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="complaint" id="complaint" name="complaint">
                        <label class="form-check-label" for="defaultCheck1">
                        Complaint
                        </label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="partnership" id="partnership" name="partnership" >
                        <label class="form-check-label" for="defaultCheck1">
                        Partnership
                        </label>
                    </div>
                </div>

Thanks for advance

Advertisement

Answer

You need to merge all orWhere together:

$data = BrandEnquiry::whereHas('brand', function($query) use ($post){
    $query->where('id_brand', '=' , $post['id_brand']);
});

$data->where(function ($key) use ($post){
    if(isset($post['question'])) 
        $key->where('subject', 'LIKE', '%'.$post['question']);
    if(isset($post['complaint']))
        $key->orWhere('subject', 'LIKE', '%'.$post['complaint']);
    if(isset($post['partnership']))
        $key->orWhere('subject', 'LIKE', '%'.$post['partnership']);
});


$data = $data->orderBy('updated_at','desc')
->with('brandOutlet')
->with('user')
->get();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement