Multiple Condition Search In laravel 9

I have a problem when try to make a multiple condition search not working properly.
what i try to achieve is to get data based on what the user input in the from includes name, date(from,to), price, and folder. The user does not have to fill in all forms, but must fill in at least 1 for the parameter.

the problems is the data wont got short by the data the user inputed

my code looks like bellow

    public function fotoTrxSearch(Request $request){

    $transaction = Foto::OrderByDesc('id');

    if($request->filled('name')){
        $transaction->where('name', 'like', "%{$request->name}%")->orderBy( 'id', 'desc');
    }
    if($request->filled('from') AND $request->filled('to')){
        $transaction->orWhereBetween('date', [$request->get('from'), $request->get('to')])->orderBy( 'id', 'desc');
    }
    if($request->filled('price')){
        $transaction->orWhere('price','like', "%{$request->price}%")->orderBy( 'id', 'desc');
    }
    if($request->filled('folder')){
        $transaction->orWhere('folder','like', "%{$request->folder}%")->orderBy( 'id', 'desc');
    }
    
    $result = new FotoCollection($transaction->paginate(50));

    return Inertia::render('Foto/FotoList',[ 'fotos' => $result]);
   
}

please help me to fix this problem or give me some reference to fix this

thankyou in advance

>Solution :

You are using "or" operator, if you want to narrow your search result, you should use "and".

For example:

 if($request->filled('price')){
     $transaction->andWhere('price','like', "%{$request->price}%")->orderBy( 'id', 'desc');
 }

Leave a Reply