从数据库,Laravel过滤搜索楼层

时间:2018-02-18 12:08:38

标签: php laravel

所以我有办公室的搜索功能,它可以搜索办公室名称,但我想要的是能够过滤楼层,就像你输入“1楼”它显示属于该楼层的办公室

enter image description here

enter image description here

以下是代码:

BuildingController.php

 public function show($id)  
    {

    $building = Building::find($id);

    $search = \Request::get('search');

   $offices = Office::where('building_id',  $id)->where('name','LIKE','%'.$search.'%')->orderBy('floor')->get();  
}

我尝试使用这种功能// $offices = Office::where('building_id', $id)->where('name','LIKE','%'.$search.'%')->orWhere('floor','LIKE','%'.$search.'%')->orderBy('floor')->get();来搜索地板,但它搞砸了我的程序,就像它显示了每个建筑物中的所有办公室,甚至是它不属于的那个办公室。

building.blade.php

{!! Form::open(['method'=> 'GET','url'=>['building',$building->id],'role'=>'search']) !!}

                       <div class="input-group col-xs-4 col-md-6" >
          <input type="text" name="search" class="form-control" placeholder="Search..." >
         <span class="input-group-btn">
           <button type="submit" class="btn btn-warning btn-md"><span class="glyphicon glyphicon-search"></span> Search</i>
           </button>
          </span>
            </div>
             {!! Form::close()!!}

1 个答案:

答案 0 :(得分:1)

使用where() closure

$offices = Office::where('building_id', $id)
    ->where(function($q) use($search) {
        $q->where('name', 'like', '%' . $search . '%')
          ->orWhere('floor', 'like', '%' . $search . '%');
    })
    ->orderBy('floor')
    ->get();