Laravel 5搜索未按预期工作

时间:2017-03-01 05:54:17

标签: php laravel search laravel-5

我是Laravel 5.4的新用户。当我输入id号需要从名为new的数据库中获取服务时,我想开发搜索。但根据此代码,它不起作用。只显示所有服务而不是与其id相关的确切值。我想要的是如果我输入id 1 ..i需要提取与id = 1相关的服务并在search.blade.phpplease中显示它帮助我!

这是我的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>

这是我的Controller UserController

public function search_code(Request $ request){

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

    }

这是我的路线

Route::post('search', 'UserController@search_code');

3 个答案:

答案 0 :(得分:0)

为什么使用where作为整数列?只需在查询中使用标准$customers = DB::table('news')->where('id', $request->search)->get(); 子句即可。

Search

我还会检查您的搜索输入的名称,现在它是search,但您可能希望将其小写为SYN

答案 1 :(得分:0)

尝试以下方法:

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

UserController中:

{{1}}

答案 2 :(得分:0)

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

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