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();
原文地址:https://www.cnblogs.com/helingfeng/p/6486872.html