Home > PHP Framework > Laravel > body text

Introducing the advanced usage of where in Laravel

藏色散人
Release: 2021-03-15 09:03:29
forward
3851 people have browsed it

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)
    Copy after login
  • 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!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template