Laravel 中的简单搜索

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42972095/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:41:55  来源:igfitidea点击:

Simple search in laravel

laravellaravel-5laravel-5.4

提问by Ovidiu G

I'm trying to build a search system in my small laravel project but considering that I'm still a beginner I can't figure how this should work..

我正在尝试在我的小 Laravel 项目中构建一个搜索系统,但考虑到我仍然是一个初学者,我无法弄清楚这应该如何工作..

Basically, I have a controller which returns an index page with all the users. In the index page I have multiple select tags where you can choose an option and filter the users.

基本上,我有一个控制器,它返回一个包含所有用户的索引页面。在索引页面中,我有多个选择标签,您可以在其中选择一个选项并过滤用户。

Here's my index method:

这是我的索引方法:

public function index(){

  $users = User::all();
  $service = Service::all();

  return view('browse.index', compact('users','service'));
}

Now, the thing is, I have a relationship built between users and services. So, each user hasMany services. With that in mind, when I'm building the search function I want to be able to access the user service. Something like this:

现在,问题是,我在用户和服务之间建立了关系。所以,每个用户都有很多服务。考虑到这一点,当我构建搜索功能时,我希望能够访问用户服务。像这样的东西:

  $category = $request->input('category');

  foreach ($users as $user) {
    $serviceFound = $user->service->where('category', 'LIKE', '%' . $category . '%');
  }

Of course this doesn't work because considering that I have a get route for my index, I don't know how to set up the form so I can use the $request.. I hope this is clear...Maybe you guys can help me with setting up the form/route and clear my mind on how should I do this...

当然这不起作用,因为考虑到我的索引有一个 get 路由,我不知道如何设置表单以便我可以使用 $request .. 我希望这很清楚......也许你们可以帮助我设置表格/路线并清除我应该如何执行此操作的想法...

回答by EddyTheDove

Build a simple form using GETas well. Simple Example

构建一个简单的表单GET。简单示例

<form action="" method="GET">
    <input type="text" name="category" required/>
    <button type="submit">Submit</button>
</form>

In your controller you do

在你的控制器中你做

public function index(Request $request){
    $category = $request->input('category');

    //now get all user and services in one go without looping using eager loading
    //In your foreach() loop, if you have 1000 users you will make 1000 queries

    $users = User::with('services', function($query) use ($category) {
         $query->where('category', 'LIKE', '%' . $category . '%');
    })->get();

    return view('browse.index', compact('users'));
}

Eager loading will work only if you have your relationships setup properly.
Keep your route unchanged the way it is now.

只有在您正确设置关系的情况下,急切加载才会起作用。
保持您的路线不变,就像现在一样。

回答by Gravy

In your controller:

在您的控制器中:

public function index(\Request $request)
{
    return $request::all();
}

In your routes:

在您的路线中:

Route::get('searchtest', 'SearchController@index');

Example url:

示例网址:

http://localhost:8000/searchtest?search=search_string&category=socks

And finally, the output

最后,输出

{
  "search": "search_string",
  "category": "socks"
}

You can also do $category = $request::get('category');to get only the category as a string.

您也$category = $request::get('category');可以只将类别作为字符串获取。