0

I'm trying to get the query with the field 'Active = S', and have the result of that have 'BusinessName lilke $request->search' or 'Label like $request->search'

I think there must be a maybe nested way to do it

Thanks for your Help! Here is the code:

   $afiliates = DB::table('ad_afiliado')
        ->select('logo_url', 'NombreComercial', 'Destacado', 'id_afiliado', 'Clave','Activo')
        ->where('Activo','=', 'S')            
        ->where('NombreComercial','like', '%' . $request->search . '%')            
        ->where('Etiqueta', 'like', '%' . $request->search . '%')
        ->orderBy('CLAVE', $request->order)
        ->paginate(9);
        

This way I'm doing force to acomplish all "where's" and I want to acomplish always 'Activo = S' , and any of 'NombreComercial like' or 'Etiqueta like'

I'm using LARAVEL 7

1
  • 4
    Share some code, what you've tried so far? Commented Oct 20, 2022 at 14:43

1 Answer 1

1

I assume you are working on User model.

$users = User::query()
        ->where('active', 'S')
        ->where(function (\Illuminate\Database\Eloquent\Builder $query) use ($request) {
            $query->orWhere('businessName', 'like', "%{$request->search}%");
            $query->orWhere('label', 'like', "%{$request->search}%");
        })
        ->get();

Please make sure to update the model and column names with the relative names to your needs.

Sign up to request clarification or add additional context in comments.

4 Comments

Sorry I didn't understand the 'query' part. I update my code. Thanks for your help!
@PepeF. reading the docs is usually a very good place to start: laravel.com/docs/9.x/queries#logical-grouping
Thanks @CornelRaiu I'll check that!
@Shailesh Thanks re read your answer, and It works, I was missing the fieldnames mine's are in spanish, Thanks both!

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.