0

I hava a dropdown color field. In my table, there is empty value for color but the problem is when I filter other color value it will show the list of table correctly but when I filter the blank value, the table return no records. Is there any way to solve this? enter image description here

Here my code so far

Eloquent

public function getColor($item_id, $color){

   $query = Color::where('item_id', '=', $item_id)
               ->orWhere('color','=',$color)
               ->paginate(10);
   return $query
}

Controller

if ($request->item_id || $request->color){
      $filter = $eloquentRepo->getColor($request->item_id, $request->color);
}
else{
     $filter = $eloquentRepo->all();
}
5
  • you can add a condition on your eloquent function to check if color field is null and query using whereNull() method laravel.com/docs/8.x/collections#method-wherenull Commented Aug 27, 2021 at 7:30
  • This question is very confusing, at least for me. Can you perhaps show what you expect when you select a valid colour and when you select the blank colour? Commented Aug 27, 2021 at 7:30
  • @archvayu i did add orWhereNull('color') it still return nothing. Commented Aug 27, 2021 at 7:45
  • @user3532758 basically if I select the blank dropdown, the table will only show item id value 2,5,6 as the color column is blank Commented Aug 27, 2021 at 7:46
  • Did you add the condition when you tried with orWhereNull? Commented Aug 27, 2021 at 8:23

1 Answer 1

1

As @archvayu mentioned, try with condition and orWhereNull method because normal where would not work when you are querying null values.

Try something like this (edited after comment):

$query = Color::when($itemId, function($q) use ($itemId){
           return $q->where('item_id', $itemId);
         })
         ->when(is_null($itemId), function($q) use ($color){
             return $q->when($color, function($q) use ($color){
                        return $q->where('color', $color);
                     })
                      ->when(is_null($color), function($q) {
                        return $q->whereNull('color');
                     });
         })
         ->paginate();
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, thank you for this but somehow it still doesn't work.
Ok, I think we need to address what you actually want again. Are you actually trying to do this: if item id is provided, just return that item with its assigned colour. And if a colour is provided without an item id, you want all items belonging to that colour (and sometimes null is treated as a valid colour for absence of colour). Am I understanding this correctly?
Yes, that is what I'm trying to achieve.
@NewbieCoder edited answer based on that. I think this should give you what you want
Hi, thank you for this! I have another question, what if I have more column like another 3 to be filter how can I make it can as single filter and also multiple filter as well? Means if user search by one dropdown it will get the result same as if user select two or more dropdown. If I use where() and orWhere() it won't return multiple selection. Feel like when() quite tough for that, is there any other way for that?
|

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.