请用laravel进行简单的搜索

时间:2018-03-16 13:55:54

标签: laravel search-form

我尝试在laravel中创建表单搜索。 当搜索标题文章的标题时,标题将出现..我是搜索标题文章形式文章表

这是我的 HomeController

...
  public function search(Request $request){
      $cari = $request->get('search');
      $Title = Article::where('title', 'LIKE', '%' .$cari . '%')->paginate(10);
      return view('/article/show', $cari);
    }

这是我的 header.blade.php

**...
<div class = "col-md-4">
      {!! Form::open(['method'=>'GET', 'url'=>'/article/show', 'role'=>'search']) !!}
      <div class= "input-group custom-search-form">
        <input type="text" class="form-control" name="search" placeholder="Judul..">
        <span class="input-group-btn">
          <span class="input-group-btn">
            <button class="btn-btn-default" type="submit"><i class="fa fa-search"></i>Cari</button>
          </span>
        </span>
        {!! Form::close()!! }
      </div>
    </div>
这是我的路线..

..
Route::get('/article/show', 'HomeController@search');

。 但是当我在搜索表单上打字时......我收到的错误就像这样

(2/2) QueryException
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'show AND is_show = TRUE and `articles`.`deleted_at` is null' at line 1 (SQL: select count(*) as aggregate from `articles` where category_id=show AND is_show = TRUE and `articles`.`deleted_at` is null)

请告诉我哪个部分错了 感谢...

3 个答案:

答案 0 :(得分:0)

您正在发送POST请求作为GET。将GET切换到POST,或者在URL中包含搜索的术语并删除搜索的Request $ request部分(Request $ request)函数,因为GET不会提供Request的实例。如果你走这条路,只需要搜索($ terms)。我个人只会切换到POST。

IE:

Route::post('/article/show', 'HomeController@search');

{!! Form::open(['method'=>'POST', 'url'=>'/arti...

答案 1 :(得分:0)

在您的表单中添加

{{ csrf_token() }}

此外,您还需要检查是否搜索&#39;有一个值,所以将它包裹在if ($request->search)

此外,看起来您可能在页面上的其他位置出现SQL错误导致此错误。

答案 2 :(得分:0)

检查你的表名,或试试这个

      public function search(Request $request)
      {
         $cari = $request->get('search');
         $data['result']= DB::table('articles')->WHERE('title', 'LIKE', '%' .$cari . '%')->paginate(10);
         return view('/article/show', $data);
      }

你的表格

       <form class="navbar-form navbar-left" method="GET" action="{{url('search')}}">
        <div class="input-group">
          <input type="text" class="form-control" placeholder="Search" name="search">
            <button class="btn btn-default" type="submit">
              <i class="fa fa-search"></i>
            </button>
        </div>
      </form>