Laravel - 使用雄辩的现场搜索设施 [英] Laravel - Search Facility on site using Eloquent

查看:186
本文介绍了Laravel - 使用雄辩的现场搜索设施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个laravel新手!并且努力寻找如何创建搜索并返回结果。



这里是我所有的代码 https://gist.github.com/anonymous/8289692



我把所有内容都放在从形式到路线的要点,控制器和模型!

  / *路由* / 
路由:: get('/ search / {q}','HomeController @ search');


/ *网页表单* /
< form class =navbar-form navbar-leftrole =searchaction =/ search / = POST >
< div class =input-group>
< input type =textclass =form-control占位符=搜索程序name =1>
< span class =input-group-btn>
< input type =submitclass =btn btn-primaryvalue =Search>
< / span>
< / div><! - / input-group - >
< / form>


/ *控制器* /
public function search($ q)
{
$ q = Input :: get('term');
$ searchTerms = explode('',$ q);
$ query = DB :: tables('wc_program');

foreach($ searchTerms as $ term)
{
$ query-> where('JobRef','LIKE','%'。$ term。'% );
}
$ results = $ query-> get();
}


/ *模型(以防万一)* /
类搜索扩展Eloquent {

protected $ table ='wc_program ;
public $ timestamps = false;
}


解决方案






您的表单使用 POST 方法,但是你的路由是一个GET路由。这就是$ code> NotFoundHttpException 。所以让你的路线如下:

  Route :: post('search','HomeController @ search'); 

没有必要参数,因为你用POST捕捉它,而不是GET!




您的输入没有名称'term',而是'1'。这可能是一个错字,但无论如何,这样做:

 < input type =textclass =form-控制占位符=搜索程序name =term> 

此外,我建议使用Laravel的URL方法来构建正确的URL:

 < form class =navbar-form navbar-leftrole =searchaction ={{URL :: to('search')} }method =post> 

或更好:

  {{Form :: open(array('url'=>'search','class'=>'navbar-form navbar-left','role'=>'search') }} 



现在,给控制器,让我们重写套件新路线:

  public function search()// no parameter now 
{
$ q =输入:: get('term');
if($ q&& $ q!=''){
$ searchTerms = explode('',$ q);
$ query = DB :: table('wc_program'); //它是DB :: table(),而不是DB :: tables

if(!empty($ searchTerms)){

foreach($ searchTerms as $ term){
$ query-> where('JobRef','LIKE','%'。$ term。'%');
}
$
$ results = $ query-> get();

dd($ results); //用于调试目的使用查看此处
}
}


I'm a laravel newbie!!! And struggling to find out how to create a search and return the results.

Here's all my code on gist https://gist.github.com/anonymous/8289692

I've put everything in the gist from the form to the route, the controller and the model!

/* Route */
Route::get('/search/{q}', 'HomeController@search');


/* Form from web page */
<form class="navbar-form navbar-left" role="search" action="/search/" method="post">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search Programmes" name="1">
<span class="input-group-btn">
<input type="submit" class="btn btn-primary" value="Search">
</span>
</div><!-- /input-group -->
</form>


/* Controller */
public function search($q)
{
$q = Input::get('term');
$searchTerms = explode(' ', $q);
$query = DB::tables('wc_program');

foreach($searchTerms as $term)
{
$query->where('JobRef', 'LIKE', '%'. $term .'%');
}
$results = $query->get();
}


/* Model (just in case) */
class Search extends Eloquent {

protected $table = 'wc_program';
public $timestamps = false;
}

解决方案

There are some issues in your code:


Your form is using POST method, but your route is a GET route. And this accounts for the NotFoundHttpException. So make your route like:

Route::post('search', 'HomeController@search');

There's no need for a parameter, since your catching it with POST anyway, not GET!


Your input doesn't have the name 'term', but '1'. That might be a typo, but anyway, make it so:

<input type="text" class="form-control" placeholder="Search Programmes" name="term">

Also, I suggest using Laravel's URL methods to build a correct url:

<form class="navbar-form navbar-left" role="search" action="{{URL::to('search')}}" method="post">

Or better:

{{Form::open(array('url' => 'search', 'class' => 'navbar-form navbar-left', 'role' => 'search')}}


Now, to the controller. Let's rewrite to suite the new route:

public function search()  //no parameter now
{
  $q = Input::get('term');
  if($q && $q != ''){
    $searchTerms = explode(' ', $q);
    $query = DB::table('wc_program');  // it's DB::table(), not DB::tables

    if(!empty($searchTerms)){

      foreach($searchTerms as $term) {
        $query->where('JobRef', 'LIKE', '%'. $term .'%');
      }
    }
    $results = $query->get();

    dd($results); // for debugging purpose. Use a View here
   }
}

这篇关于Laravel - 使用雄辩的现场搜索设施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆