Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

310
Vistas
How to use multiple OR,AND condition in Laravel queries

I need help for query in laravel

My Custom Query: (Return Correct Result)

Select * FROM events WHERE status = 0 AND (type="public" or type = "private")

how to write this query in Laravel.

Event::where('status' , 0)->where("type" , "private")->orWhere('type' , "public")->get();

But it's returning all public events that status is not 0 as well.

I am using Laravel 5.4

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

Pass closure into the where():

Event::where('status' , 0)
     ->where(function($q) {
         $q->where('type', 'private')
           ->orWhere('type', 'public');
     })
     ->get();

https://laravel.com/docs/5.4/queries#parameter-grouping

over 4 years ago · Santiago Trujillo Denunciar

0

In your case you can just rewrite the query...

select * FROM `events` WHERE `status` = 0 AND `type` IN ("public", "private");

And with Eloquent:

$events = Event::where('status', 0)
    ->whereIn('type', ['public', 'private'])
    ->get();

When you want to have a grouped OR/AND, use a closure:

$events = Event::where('status', 0)
    ->where(function($query) {
        $query->where('type', 'public')
            ->orWhere('type', 'private');
    })->get();
over 4 years ago · Santiago Trujillo Denunciar

0

Use this

$event = Event::where('status' , 0);

$event = $event->where("type" , "private")->orWhere('type' , "public")->get();

or this

Event::where('status' , 0)
     ->where(function($result) {
         $result->where("type" , "private")
           ->orWhere('type' , "public");
     })
     ->get();
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda