2021-01-11|閱讀時間 ‧ 約 2 分鐘

Laravel ORM where中加入括號

直接從程式碼來看,假設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
分享至
成為作者繼續創作的動力吧!
小弟是一位軟體工程師,樂於幫助他人,撰寫技術文章除了幫助自己複習以外,也希望可以幫助到他人,若文章內容有誤,還請大大不吝給予指教!
從 Google News 追蹤更多 vocus 的最新精選內容從 Google News 追蹤更多 vocus 的最新精選內容

發表回應

成為會員 後即可發表留言