2015-11-19 31 views
0

我怎樣才能做到這一點,就像從「表」中得到的,其中「student_staff」等於「學生」「AND」其中「部門」等於「全部」或「部門」等於「以「department1?任何幫助,想法,線索,建議,意見?AND laravel中的語句5口才

到目前爲止,我曾嘗試

$notifications = notification::where('student_staff', '=', 'student') 
    ->where('department', '=', 'all') 
    ->orWhere('department', '=', 'department1')->get(); 
+1

默認值爲「and」。 –

+0

@Thomas Kim:對不起,我不太明白,你能用你的答案延長我的代碼嗎? –

+0

我編輯了我的答案,下面是你修改過的問題。我認爲你想要的就是所謂的「參數分組」。 –

回答

3

爲了回答您的評論的問題,沒有什麼可擴展,默認爲例如,讓我們看看你寫的內容:

從「表」,其中「名」等於「傑森」並在「時代」選擇是否等於「16」

默認where條款已做了你想要的東西。

Model::where('name', '=', 'jason') 
    ->where('age', '=', 16) 
    ->get(); 

這變成沿select * from `TABLE` where `name` = jason and `age` = 16線的東西。

如果你看一看無論是在雄辯/查詢生成器的where方法,你會看到:

public function where($column, $operator = null, $value = null, $boolean = 'and') 

你可以從第四個參數看,默認已經是「和」。在相關說明中,如果需要,您可以覆蓋它並輸入'或'。

Model::where('name', '=', 'jason') 
    ->where('age', '=', 16, 'or') 
    ->get(); 

還有一個orWhere方法,雖然你這樣做。

Model::where('name', '=', 'jason') 
    ->orWhere('age', '=', 16) 
    ->get(); 

這會變成這樣:

select * from `TABLE` where `name` = jason or `age` = 16

編輯:只是注意到你編輯你的問題。

「表」,其中「student_staff」等於「學生」「和」位置「部門」等於「全」或「部門」等於「department1

我想什麼你想其實是這樣的:"student_staff" is equal to "student" 'AND' (where "department" is equal to "all" or "department" is equal to "department1)

這就是所謂的參數組文件在這裏:http://laravel.com/docs/5.1/queries#advanced-where-clauses

爲了達到這一說法,這將是這樣的:

$models = Model::where('student_staff', '=', 'student') 
       ->where(function($query) { 
        $query->where('department', '=', 'all') 
          ->orWhere('department', '=', 'department1'); 
       })->get();