Laravel5 構造器高級查詢條件寫法


 1 <?php
 2 
 3 #DB 高級查詢
 4 // select * from table where A and B or C
 5 $all_data = DB::table("shopnc_goods_common")
 6     ->where("base_goods_commonid", -1)
 7     ->where('goods_name', 'like', '%' . $keywords . '%')
 8     ->orWhere('goods_jingle', 'like', '%' . $keywords . '%')
 9     ->select("goods_commonid")->get();
10 // select * from table A and B
11 $users = DB::table('users')
12     ->whereColumn([
13         ['first_name', '=', 'last_name'],
14         ['updated_at', '>', 'created_at']
15     ])->get();
16 
17 // select * from table A and ( B or C )
18 $all_data = DB::table("shopnc_goods_common")
19     ->where("base_goods_commonid", -1)
20     ->where(function ($query) use ($keywords) {  //閉包
21         $query->where('goods_name', 'like', '%' . $keywords . '%')
22             ->orWhere('goods_jingle', 'like', '%' . $keywords . '%');
23     })
24     ->select("goods_commonid")->get();

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM