2017-04-17 72 views
0

我剛接觸laravel 5.4,需要創建多屬性搜索。我搜索之前的 。 enter image description hereLaravel 5搜索功能

之後的搜索結果與之前的結果相同。 enter image description here

這是我的控制器。

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); 

      } 

這是我的看法。

<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> 

在我寫的路線,不過,我覺得問題是途經此地是兩個擊潰

Route::group(['middleware' => ['web']], function() { 
    Route::resource('/userregister', 'UserRegisterController'); 

}); 
Route::post('search', '[email protected]_code'); 

任何人都可以建議我通過搜索該得到的結果嗎?

+0

什麼是確切的問題你在面對? –

+0

搜索不起作用。例如,如果我輸入id 1兩個結果都顯示,但它們位於兩個不同的id中 – Dasun

+0

現在是顯示所有記錄嗎? – manian

回答

0

變化選擇下拉列表中的值類似下面

<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> 

一個建議, 如果只有一個條件,可以再使用「否則,如果」像下面,

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

非常感謝sir.it工作正常:) – Dasun

+0

我很高興它的工作。快樂編碼:) – manian