下面由Laravel教學專欄跟大家講解laravel orWhere怎麼用,希望對大家有幫助!
laravel框架下where 條件查詢很簡單,這裡就不寫了,這裡主要記錄一下平常不怎麼用的orWhere 多條件or 查詢語句的拼接,示例如下:
#(1 )SQL 語句:
select `id` from `user` where (`email` = 'admin@phpernote.com' and `password` = '123456') or (`mobile` = '13681127231' and `password` = '123456');
laravel model 語句:
User::select(['id']) ->where(function ($query) use ($userName, $password) { $query->where('password', '=', '123456')->where('email', '=', 'admin@phpernote.com'); }) ->orWhere(function ($query) use ($userName, $password) { $query->where('password', '=', '123456')->where('mobile', '=', '13681127231'); }) ->first();
(2)SQL 語句:
select count(*) from `user` where `type` = 1 and `valid_type` = 2 and (`valid_end` < 1560738570 or `valid_begin` > 1560738570);
laravel model 語句:
$model = User::where('type', 1)->where('valid_type', '=', 2)->where(function ($query) { $query->where('valid_end', '<', 1560738570)->orWhere(function ($query) { $query->where('valid_begin', '>', 1560738570); }); })->first();
(3)SQL語句:
select count(*) from `user` where `valid_type` = 2 or (`valid_type` = 3 and (`valid_end` < 1560738570 or `valid_begin` > 1560738570));
laravel model 語句:
$model = User::where(function ($query) { $query->where('valid_type', 2); })->orWhere(function ($query) { $query->where('valid_type', '=', 3)->where(function ($query) { $query->where('valid_end', '<', 1560738570)->orWhere(function ($query) { $query->where('valid_begin', '>', 1560738570); }); }); })->first();
#相關推薦:最新的五個Laravel影片教學
以上是laravel orWhere怎麼用? (附程式碼範例)的詳細內容。更多資訊請關注PHP中文網其他相關文章!