'The result returned is incorrect when retrieving data in the database

I am using laravel to create a website and I am getting an error that getting data from database does not return correct results, what did I do wrong

public function search(Request $request){
    $filter = $request->get('filter');
    $search = $request->get('q');
    $items = [];
    if ($filter === 'default' || empty($filter)){
        $query = DB::table(DBNAME::$DB_NAME)->
        where('title','LIKE','%'.$search.'%')->
        orWhere('description','LIKE','%'.$search.'%')->
        orWhere('tags','LIKE','%'.$search.'%')->
        where('active',true)->orderByDesc('id');
        $items = $this->getData($query);
    }elseif($filter === 'free'){
        $query = DB::table(DBNAME::$DB_NAME)->
        where('active',true)->
        where('price',0)->
        where('title','LIKE','%'.$search.'%')->
        orWhere('description','LIKE','%'.$search.'%')->
        orWhere('tags','LIKE','%'.$search.'%')->orderByDesc('id');
        $items = $this->getData($query);
    }elseif($filter === 'low_to_high_price'){
        $query = DB::table(DBNAME::$DB_NAME)->
        where('title','LIKE','%'.$search.'%')->
        orWhere('description','LIKE','%'.$search.'%')->
        orWhere('tags','LIKE','%'.$search.'%')->
        where('active',true)->orderBy('price');
        $items = $this->getData($query);
    }elseif($filter === 'high_to_low_price'){
        $query = DB::table(DBNAME::$DB_NAME)->
        where('title','LIKE','%'.$search.'%')->
        orWhere('description','LIKE','%'.$search.'%')->
        orWhere('tags','LIKE','%'.$search.'%')->
        where('active',true)->orderByDesc('price');
        $items = $this->getData($query);
    }
    elseif($filter === 'from_new_to_old'){
        $query = DB::table(DBNAME::$DB_NAME)->
        where('title','LIKE','%'.$search.'%')->
        orWhere('description','LIKE','%'.$search.'%')->
        orWhere('tags','LIKE','%'.$search.'%')->
        where('active',true)->orderByDesc('created_at');
        $items = $this->getData($query);
    }elseif($filter === 'from_old_to_new'){
        $query = DB::table(DBNAME::$DB_NAME)->
        where('title','LIKE','%'.$search.'%')->
        orWhere('description','LIKE','%'.$search.'%')->
        orWhere('tags','LIKE','%'.$search.'%')->
        where('active',true)->orderBy('created_at');
        $items = $this->getData($query);
    }
    return APP::createResponseJson(true,__('strings.success'),$items);
}
private function getData($query,$i=0,$items=[]){
    foreach ($query->get() as $item){
        $items[$i]['title']=$item->title;
        $items[$i]['thumbnail']=$item->thumbnail;
        $items[$i]['description']=$item->description;
        $items[$i]['price']=$item->price;
        $items[$i]['sale']=$item->sale;
        $items[$i]['tags']=$item->tags;
        $items[$i]['created_at']=$item->created_at;
        $items[$i]['updated_at']=$item->updated_at;
        $i++;
    }
    return $items;
}

I have sent requests to the following links:

http://localhost:8000/api/search

http://localhost:8000/api/search?filter=free

http://localhost:8000/api/search?filter=default&q=abcd

I got the same result

Currently in my database there is only 1 data

{
"status": true,
"message": "Success",
"data": [{
    "title": "example",
    "thumbnail": null,
    "description": "description example",
    "price": 100,
    "sale": 0,
    "tags": "tag1,tag2,tag3",
    "created_at": "2022-04-12 13:34:22",
    "updated_at": null
}]
}


Solution 1:[1]

I found the solution:

replace:

$query = DB::table(DBNAME::$DB_NAME)->
    where('title','LIKE','%'.$search.'%')->
    orWhere('description','LIKE','%'.$search.'%')->
    orWhere('tags','LIKE','%'.$search.'%')->
    where('active',true)->orderByDesc('id');

to:

$query = DB::table(DBNAME::$DB_NAME)->
            where(function($query) use ($search){
            $query->orWhere('title','LIKE','%'.$search.'%')->
            orWhere('description','LIKE','%'.$search.'%')->
            orWhere('tags','LIKE','%'.$search.'%');
         
        })->where('active',true)
       ->orderByDesc('id');

and now it's working fine

I found it at: Multiple orWhere Condition in Laravel Eloquent

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hong ck