Home > OS >  Laravel Controller Nested 'Where' Conditions
Laravel Controller Nested 'Where' Conditions

Time:10-21

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

CodePudding user response:

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.

  • Related