0

I building an application for ads/properties in Laravel. I have a search form with filters that are checkboxes. I am having a problem when I select two options from the same request for example supply, demand that are PropertyBidAsk I get only results from demand and not both, also I would like to keep both checked after form submission. Supply and demand are values in the category column in the categories table. Any help is appreciated. Here is my code.

CategoryController.php:

public function search(Request $request, Property $property)
{
    $category = $property->category;

    $query = Property::query();

    if ($request->has('propertyBidAsk')) {
        $request->get('propertyBidAsk');
    }

    if ($request->propertyBidAsK == 'supply' && $request->propertyBidAsk== 'demand') {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . $request->propertyBidAsk . '%');
        });
    } elseif ($request->propertyBidAsk == 'supply') {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'supply' . '%');
        });
    } else if ($request->propertyBidAsk == 'demand') {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'demand' . '%');
        });
    }

    $results = $query->paginate(6);

    return view('search', compact('category', 'results', 'request'));
}

search.blade.php:

<div class="col-md-2 mb-6">
    <h5>Payment Method</h4>
    <div class="d-block my-3">
        <div class="custom-control custom-checkbox">
            <input id="supply" name="propertyBidAsk" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>
            <label class="custom-control-label" for="supply">supply</label>
        </div>
        <div class="custom-control custom-checkbox">
            <input id="demand" name="propertyBidAsk" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>
            <label class="custom-control-label" for="demand">demand</label>
        </div>
    </div>
</div>
6
  • 1
    $request->propertyBidAsK == 'supply' && $request->propertyBidAsk== 'demand' How could a single string possibly have two distinct values? Commented Jul 19, 2019 at 7:05
  • maybe you mean || instead of && ? Commented Jul 19, 2019 at 7:08
  • @Jerodev I know, but I tried everything and nothing works. This is just for someone to get the idea of what I'm trying to do and to help me if can. I don't know how to solve this Commented Jul 19, 2019 at 7:08
  • Maybe you can remove your if condition and just put this $query->where('category', 'like', '%' . $request->propertyBidAsk . '%'); Commented Jul 19, 2019 at 7:10
  • and try to use dd($request->propertyBidAsk); if your request has the right value you want Commented Jul 19, 2019 at 7:12

2 Answers 2

2

To get the multi select output with same name attr, update name of your checkbox input to propertyBidAsk[] as array. You will get input array.

<input id="supply" name="propertyBidAsk[]" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>


<input id="demand" name="propertyBidAsk[]" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>

In controller will get the value by.

public function search(Request $request, Property $property)
{
    $category = $property->category;

    $query = Property::query();

    if ($request->has('propertyBidAsk')) {
        $request->get('propertyBidAsk');
    }


    $propertyBidAsk = $request->input('propertyBidAsk'); //$propertyBidAsk will get in array

    if (in_array('supply', $propertyBidAsk)  && in_array('demand', $propertyBidAsk)){
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%demand%')->orWhere('category', 'like', '%supply%')
        });
    } else if(in_array('supply', $propertyBidAsk)){
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'supply' . '%');
        });
    } else  if(in_array('demand', $propertyBidAsk)){
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'demand' . '%');
        });
    }

    $results = $query->paginate(6);

    return view('search', compact('category', 'results', 'request'));
}
Sign up to request clarification or add additional context in comments.

6 Comments

I get this error now 'Array to string conversion'.
For multi checkbox need to pass the name attr with array. print_r the $propertyBidAsk you will get the result based on checked value
It works now I get correct results. Just my checkboxes don't remain checked after submission. How to solve that? I will accept your answer of course. Thank you
you have need to change the @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>
|
1

As @Shivendra Singh answer you can do that and you can do this also

<input id="supply" name="propertyBidAsk1" value="supply" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'supply') checked @endif>


<input id="demand" name="propertyBidAsk2" value="demand" type="checkbox" class="custom-control-input" @if(old('propertyBidAsk', $request->propertyBidAsk ?? 'default') === 'demand') checked @endif>

You see you can't have same name in an form element it will always get the first value.

then in you controller

if (!empty($request->propertyBidAsK1) && !empty($request->propertyBidAsk2) {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . $request->propertyBidAsk1 . '%')
                  ->orWhere('category', 'like', '%' . $request->propertyBidAsk2 . '%');
        });
    } elseif (!empty($request->propertyBidAsk1)) {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'supply' . '%');
        });
    } else if (!empty($request->propertyBidAsk2)) {
        $query->whereHas('category', function ($query) use ($request) {
            $query->where('category', 'like', '%' . 'demand' . '%');
        });
    }

Hope it helps

1 Comment

With this it keeps both checkboxes checked but I get only results from supply and not demand

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.