Laravel中的where高级利用要领实例讲授

有时候项目中需要举办多个字段搜索就可以用到此要领

在Laravel中的可以同时利用多个where,所以我们可以每个字段分派一个where()

然后在每个where()中去闭包判定

$username = '';// 收货人姓名 $hospital_id = ''; // 医院id # 判定是否有姓名搜索 if (!empty($request->username)) { $username = $request->username; } # 判定是否有医院搜索 if (!empty($request->hospital_id)) { $hospital_id = $request->hospital_id; } # 执行 $data = DB::table('test') ->where(function($query)use($username){ # 举办判定 if (!empty($username)) { $query->where('username','Like',"%$username%"); } }) ->where(function($query)use($hospital_id){ # 举办判定 if (!empty($hospital_id)) { $query->where('hospital_id','=',$hospital_id); } }) ->get() ->toArray(); dd($data)

到此这篇关于Laravel中的where高级利用要领实例讲授的文章就先容到这了,更多相关Laravel中的where高级利用要领内容请搜索剧本之家以前的文章或继承欣赏下面的相关文章但愿各人今后多多支持剧本之家!

您大概感乐趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsfyxz.html