Laravel ORM where中加入括號

2021/01/11閱讀時間約 1 分鐘
直接從程式碼來看,假設code這樣寫:
$this->customer->select(['id'])
->where('cell_1', '=', $cell_1)
->orWhere(function ($query) use ($cell_2) {
 $query->whereNotNull('cell_2');
 $query->where('cell_2', '=', $cell_2);
})
->orWhere('email', '=', $email)
->get()->first();

轉換成SQL其實就是這樣:
select `id` from `customer` where `cell_1` = ? or (`cell_2` is not null and `cell_2` = ?) or `email` = ?
加入括號其實就是使用閉包的寫法!
本筆記參考:
1. https://www.weiyuan.com.tw/article/25
為什麼會看到廣告
20會員
161內容數
留言0
查看全部
發表第一個留言支持創作者!