Question: I have a table Products(id,category\_id,name);

I want to get a result like this query:

```
SELECT * FROM Products WHERE category_id=$category_id OR $category_id=0;

```

When I assign $category\_id with 0 value => above query will return all records.

How do I write that query in Laravel?

I've try this:

```
Products::where(function($query) use ($category_id){
         $query->where("category_id",$category_id)
        ->orWhere($category_id,0)
})->get();

```

But, Error:

It look like:

```
SELECT
    *
FROM
    `products` 
WHERE
    ( `product_category_id` = 0 OR `0` = 0 )

```

And Print Error: `Column not found: 1054 Unknown column '0' in 'where clause'`

How to fix: `'0' = 0` to `0 = 0`?

Thanks!

Answer: 
The other way would be like this 

```
Map<Integer, Integer> temp = new HashMap<>();
int maxCount = 0;
  for (Integer i : list) {
   maxCount = Integer.max(maxCount , temp.merge(i, 1, Integer::sum));
  }
int finalMax = maxCount;
temp.values().removeIf(v->v!= finalMax);

```

or 

---

```
 Map<Integer, Integer> temp = new HashMap<>();
    int maxCount = 0;
    int maxKey = 0;
    for (Integer i : list) {
        int count = temp.merge(i, 1, Integer::sum);
        if (maxCount < count)
            maxKey = i;
        maxCount = Integer.max(maxCount , count);
    }
    System.out.println(maxKey + " :" + maxCount);

```
You can use Apache [Bag](https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/Bag.html)

> 
> Defines a collection that counts the number of times an object appears in the collection.
>  Suppose you have a Bag that contains {a, a, b, c}. Calling getCount(Object) on a would return 2, while calling uniqueSet() would return {a, b, c}.
> 
> 
> 

[Example](https://www.baeldung.com/apache-commons-bag)

> 
> 
> ```
>  Bag<Integer> bag = new HashBag<>(
>  Arrays.asList(1, 2, 3, 3, 3, 1, 4));         
>  assertThat(2, equalTo(bag.getCount(1)));
> 
> ```
> 
>