php 使用 Laravel 4 创建搜索功能
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19612180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating search functionality with Laravel 4
提问by Mitch
I am trying to create a way for users to search through all the products on a website. When they search for "burton snowboards", I only want the snowboards with the brand burton to appear in the results. But if they searched only "burton", then all products with the brand burton should appear.
我正在尝试为用户创建一种搜索网站上所有产品的方法。当他们搜索“burton 滑雪板”时,我只希望带有 burton 品牌的滑雪板出现在结果中。但是如果他们只搜索“burton”,那么所有带有 burton 品牌的产品都应该出现。
This is what I have attempted to write but isn't working for multiple reasons.
这就是我试图写的,但由于多种原因没有奏效。
Controller:
控制器:
public function search(){
$input = Input::all();
$v= Validator::make($input, Product::$rules);
if($v->passes())
{
$searchTerms = explode(' ', $input);
$searchTermBits = array();
foreach ($searchTerms as $term) {
$term = trim($term);
if (!empty($term)){
$searchTermBits[] = "search LIKE '%$term%'";
}
}
$result = DB::table('products')
->select('*')
->whereRaw(". implode(' AND ', $searchTermBits) . ")
->get();
return View::make('layouts/search', compact('result'));
}
return Redirect::route('/');
}
I am trying to recreate the first solution given for this stackoverflow.com problem
我正在尝试重新创建针对此stackoverflow.com 问题给出的第一个解决方案
The first problem I have identified is that i'm trying to explode the $input
, but it's already an array. So i'm not sure how to go about fixing that. And the way I have written the ->whereRaw(". implode(' AND ', $searchTermBits) . ")
, i'm sure isn't correct. I'm not sure how to fix these problems though, any insights or solutions will be greatly appreciated.
我发现的第一个问题是我试图爆炸$input
,但它已经是一个数组。所以我不知道如何解决这个问题。而我写的方式->whereRaw(". implode(' AND ', $searchTermBits) . ")
,我肯定是不正确的。我不确定如何解决这些问题,任何见解或解决方案将不胜感激。
回答by afarazit
You'll need to get the terms from your input field and loop through all of them while building your DB query. You'll also need to set the table field in which you want the terms to be searched, in this the example the table field is name
. Here's an untested example but you'll get the idea.
您需要从输入字段中获取术语并在构建数据库查询时遍历所有术语。您还需要设置要在其中搜索术语的表格字段,在此示例中,表格字段为name
。这是一个未经测试的示例,但您会明白的。
public function search() {
$q = Input::get('myInputField');
$searchTerms = explode(' ', $q);
$query = DB::table('products');
foreach($searchTerms as $term)
{
$query->where('name', 'LIKE', '%'. $term .'%');
}
$results = $query->get();
}
回答by Nicolás López
I made a plugin for laravel to make this! Hope you use it and give feedback. You can specify the relevance for each column and use information from other tables
我为 laravel 制作了一个插件来做到这一点!希望您使用它并提供反馈。您可以指定每列的相关性并使用其他表中的信息
https://github.com/nicolaslopezj/searchable
https://github.com/nicolaslopezj/searchable
You can search like this
你可以这样搜索
$products = Product::search($query)->get();
回答by Qamar Uzman
Controller Function 1
控制器功能 1
public function search() {
$q = Input::get('searchKeyWords');
$users = DB::table('users');
$results = $users->where('name', 'LIKE', '%'. $q .'%')
->orWhere('email', 'LIKE', '%'. $q .'%')
->orWhere('password', 'LIKE', '%'. $q .'%')
->get();
return View::make('users.search')->with('users', $results);
}
Controller Function 2
控制器功能 2
public function search() {
$q = Input::get('searchKeyWords');
$results = User::where('name', 'LIKE', '%'. $q .'%')
->orWhere('email', 'LIKE', '%'. $q .'%')
->orWhere('password', 'LIKE', '%'. $q .'%')
->get();
return View::make('users.search')->with('users', $results);
}
Controller Function 3
控制器功能 3
public function search() {
$results = User::where('name', 'LIKE', '%'. Input::get('searchKeyWords') .'%')
->orWhere('email', 'LIKE', '%'. Input::get('searchKeyWords') .'%')
->orWhere('password', 'LIKE', '%'. Input::get('searchKeyWords') .'%')
->get();
return View::make('users.search')->with('users', $results);
}
回答by Aditya Kresna Permana
This is sample when I try using Eloquent with many search terms
这是我尝试将 Eloquent 与许多搜索词一起使用时的示例
public function search() {
$q = Input::get('myInputField');
$searchTerms = explode(' ', $q);
$query = $article->where('title', 'LIKE', "%{$keyword}%")
->orWhere('description', 'LIKE', "%{$keyword}%");
foreach($searchTerms as $term)
{
$query = $query->orWhere('name', 'LIKE', '%'. $term .'%');
}
$results = $query->get();
}
You can also change ->get()
function to other results like ->paginate()
, and it should be like $results = $query->paginate(10);
您还可以将->get()
功能更改为其他结果,例如->paginate()
,它应该像$results = $query->paginate(10);