【问题标题】:HOw to make simple search with laravel如何使用 laravel 进行简单的搜索
【发布时间】:2018-03-16 13:55:53
【问题描述】:

我尝试在 laravel 中创建表单搜索.. 当title文章的标题被搜索..然后标题会出现..我在搜索title文章表格文章表

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

.

请告诉我哪一部分是错的 谢谢...

【问题讨论】:

  • 其中 category_id=show
  • 返回什么查询?

标签: laravel search-form


【解决方案1】:

您正在以 GET 形式发送 POST 请求。将 GET 切换为 POST,或者在 URL 中包含搜索词并删除搜索(Request $request)函数的 Request $request 部分,因为 GET 不会提供 Request 的实例。如果你走那条路,只需让它 search($terms) 代替。不过,我个人会切换到 POST 。

IE:

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

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

【讨论】:

  • (1/1) MethodNotAllowedHttpException.. 我可以尝试 swith get 发布并得到错误
  • 您可以通过 GET 获取请求
【解决方案2】:

在您的表单中添加

{{ csrf_token() }}

您还需要检查“搜索”是否有值,因此将其包裹在 if ($request-&gt;search) 周围

此外,您可能在页面上的其他地方出现了 SQL 错误,这就是造成这种情况的原因。

【讨论】:

  • 函数中的代码。虽然,它不需要。只是额外的验证。
  • 此错误与此无关。它在别的地方
  • 根据我上面的代码,根据您的说法.. 出了什么问题??.. 我有关系表.. 带有表格类别的表格文章是关系..
  • 我不知道我没有看到你的代码。此外,您将在函数中返回用户输入的内容。甚至不是 SQL 数据的结果。
【解决方案3】:

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

      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>

【讨论】:

  • SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“show AND is_show = TRUE and articles.deleted_at is null”附近使用正确的语法(SQL:select count(*) as aggregate from @987654325 @ where category_id=show AND is_show = TRUE and articles.deleted_at 为空)
  • 你的数据库使用 sqlserver 吗?我的建议是使用 mysql,因为如果你的项目使用框架并且你的数据库是 sqlserver,你会遇到很多麻烦。使用mysql更好,更容易
  • deleted_at 来自哪里?这是您在文章表中的姓名列之一吗?
猜你喜欢
  • 1970-01-01
  • 2012-11-12
  • 2011-04-07
  • 2011-08-10
  • 2016-04-22
  • 2011-11-07
  • 1970-01-01
  • 1970-01-01
  • 2013-12-27
相关资源
最近更新 更多