Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

    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');
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading