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