2017-03-01 102 views
0

我是Laravel 5.4.i的新用戶當我輸入id號時想開發搜索需要從數據庫中獲取名爲new.but的服務,但根據此代碼它不起作用。只是顯示所有的服務沒有確切的價值相關的id.All我想要的是,如果我輸入ID 1 ..我需要取出相關的服務id = 1並顯示它在search.blade.phpplease幫助我!Laravel 5搜索不按預期方式工作

這裏是我的Search.blade.php

<!DOCTYPE html> 
<html lang="{{ config('app.locale') }}"> 
    <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
     <meta name="viewport" content="width=device-width, initial-scale=1"> 

     <title>Laravel</title> 

     <!-- Fonts --> 
     <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css"> 

     <!-- Styles --> 

    </head> 
    <body> 
     <form action="search" method="post"> 
     <label>Search by Id!</label><br> 
     <input type="text" name="Search" /><br> 
     <input type="hidden" value="{{ csrf_token() }}" name="_token" /> 
     <input type="submit" name="submit" value="Search"> 
     </form> 



     <table class="table table-bordered table-hover" > 

      <thead> 
       <th>Name</th> 
      </thead> 
      <tbody> 
       @foreach($customers as $customer) 

        <td>{{ $customer->service }}</td> 

       @endforeach 
      </tbody> 
     </table> 

    </body> 
</html> 

這裏是我的控制器UserController的

公共職能search_code(請求$要求){

$query = $request->search; 
    $customers = DB::table('news')->where('id', 'LIKE',"%$query%")->get(); 
    return view('search' , compact('search', 'customers')); 

    } 

這裏是我的路線

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

在你的控制器中使用whereId(「$ query」)。 – Saman

回答

0

爲什麼使用LIKE作爲整數列?只需在查詢中使用標準的where子句。

我還要檢查你的搜索輸入的名稱,現在它是Search但你可能要小寫它只是search

+0

非常感謝你..現在它工作正常 – Dasun

0

嘗試以下操作:

<form action="search" method="post" action="{{ url(/search) }}"> 
// pass the route in action attribute of form tag 

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

UserController中:

public function search() 
{ 
    $Search = Input::get('Search'); 
    // search logic here 
} 
0

變化從

return view('search' , compact('search', 'customers')); 

return view('search')->with('customers',$customers); 
+0

非常感謝你,我也做了這個改變。 – Dasun