Home  >  Article  >  PHP Framework  >  Introducing the advanced usage of where in Laravel

Introducing the advanced usage of where in Laravel

藏色散人
藏色散人forward
2021-03-11 11:26:533838browse

The following tutorial column will introduce to you the advanced usage of where in Laravel. I hope it will be helpful to friends in need!

Introducing the advanced usage of where in Laravel

Sometimes you need to search in multiple fields in the project. This method

in Laravel can use multiple wheres at the same time, so we can assign a where() to each field
and then remove the closure in each where() Judgment
  • $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)
  • Recommendation:
  • The latest five Laravel video tutorials

The above is the detailed content of Introducing the advanced usage of where in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete