2017-04-13 16 views
6

Tôi cần giúp cho truy vấn trong laravelLàm thế nào để sử dụng nhiều OR, AND tình trạng Laravel truy vấn

truy vấn tùy chỉnh của tôi: (Return quả đúng)

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

làm thế nào để viết truy vấn này trong Laravel.

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

Nhưng nó trả về tất cả các sự kiện công khai mà trạng thái không phải là 0.

Tôi đang sử dụng Laravel 5.4

Trả lời

1

Sử dụng này

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

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

hay này

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

cũng nhờ công việc này. –

0

Trong trường hợp của bạn, bạn chỉ có thể viết lại các truy vấn ...

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

Và với hùng biện:

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

Khi bạn muốn có một nhóm OR/AND, sử dụng một kết thúc:

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

Hãy thử điều này. Nó làm việc cho tôi.

$rand_word=Session::get('rand_word'); 
$questions =DB::table('questions') 
    ->where('word_id',$rand_word) 
    ->where('lesson_id',$lesson_id) 
    ->whereIn('type', ['sel_voice', 'listening']) 
    ->get(); 
Các vấn đề liên quan