首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Laravel 5搜寻档案无法运作

Laravel 5搜寻档案无法运作
EN

Stack Overflow用户
提问于 2017-04-17 10:58:10
回答 1查看 32关注 0票数 0

我对laravel 5.4很陌生,需要创建多属性搜索。在我搜索之前。

搜索后的结果和以前一样。

这是我的控制器。

代码语言:javascript
复制
 public function search_code(Request $request){
    $query = $request->search;
    $queryType = $request->institute; // 'id' or 'name'
    $items = DB::table('registerdetails');        

    if($queryType == 'id'){
      $items = $items->where('id', 'LIKE',"%$query%");
    }
    if($queryType == 'full_name'){
      $items = $items->where('full_name', 'LIKE',"%$query%");
    }
   $items = $items->get();

    return view('registeredusers.index')->with('items',$items);

            }

这是我的看法。

代码语言:javascript
复制
 <form action="search" method="post" class="form-inline">          
   <select name="institute" id="institute">
    <option selected="selected" value="Trainee Id">Trainee Id</option>
    <option value="Trainee Name">Trainee Name</option>
  <label for="Search">Name</label>
</select>  
       <input type="text" name="search" /><br>
       <input type="hidden" value="{{ csrf_token() }}" name="_token" />
       <input type="submit" name="submit" value="Search">
       </form>

</div>
</div>
</div>
            <div class="panel panel-default">

            <div class="panel-body">

                  <table class="table table-striped">
                  <thead>

                    <th>Full Name</th>
                    <th>Name with initials</th>
                    <th>National ID Number</th>
                    <th>Date Of Birth</th>

                  </thead>

                  <tbody>
                    @foreach($items as $item)

                     <tr>

                  <td>{{ $item->full_name }}</td>
                  <td>{{ $item->name_with_initials }}</td>
                  <td>{{ $item->nic_no }}</td>
                  <td>{{ $item->date_of_birth }}</td>
              </tr>
                    @endforeach

                  </tbody>

          </table>

            </div>
        </div>

在我写的路线上,我想问题在这条路上,这是两条路

代码语言:javascript
复制
    Route::group(['middleware' => ['web']], function () {
    Route::resource('/userregister', 'UserRegisterController');

});
Route::post('search', 'UserRegisterController@search_code');

有人能建议我通过搜索这个得到结果吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-17 11:14:04

更改选择下拉列表中的值,如下所示

代码语言:javascript
复制
<select name="institute" id="institute">
  <option selected="selected" value="id">Trainee Id</option>
  <option value="full_name">Trainee Name</option>
  <label for="Search">Name</label>
</select>

还有一个建议,如果只有一个条件是可行的,那就用下面的“如果”,

代码语言:javascript
复制
if($queryType == 'id'){
  $items = $items->where('id', 'LIKE',"%$query%");
}
else if($queryType == 'full_name'){
  $items = $items->where('full_name', 'LIKE',"%$query%");
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43450118

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档