简体   繁体   English

关系中的 Laravel 侦察搜索

[英]Laravel scout search in relationships

I can't seem to figure out how to search in 2 tables within 1 query in Laravel scout.我似乎无法弄清楚如何在 Laravel scout 中的 1 个查询中的 2 个表中进行搜索。 For specific reasons that are not important right now, I don't want to just run raw queries.出于目前不重要的特定原因,我不想只运行原始查询。 Right now I have this to search:现在我有这个要搜索:

My relationship:我的关系:

namespace App;
use Laravel\Scout\Searchable;
class Users extends Model
{
    use Searchable;
   public function searchableAs(){
      return 'users_index';
   }
   public function toSearchableArray(){
      return $this->toArray();
   }
   public function originalUser(){
      return $this->belongsTo(OriginalUser::class);
   }
}

And this is the controller:这是控制器:

use App\OriginalUser;
use App\Users;
use App\Groups;

class SomeController extends Controller{

   public function index(){
     $group = Group::where('group_id',1)->first();
     return Users::search('something')->where('group_id',$group->id)->get();
   }
}

Now this returns the user that has 'something' in one of the columns, which is good like this:现在这将返回在其中一列中具有“某物”的用户,如下所示:

   [
      {
        "id": "1",
        "original_user_id": "1",
        "username": "something"
        "original_user": {
          "id": "1",
          "username":"test"
        }
      }
    ]

But I need my code to also return this record:但我需要我的代码也返回这条记录:

   [
      {
        "id": "2",
        "original_user_id": "2",
        "username": "nope"
        "original_user": {
          "id": "2",
          "username": "something"
        }
      }
    ]

So when it matches in the relationship of the result, I want that record to be returned as well.因此,当它与结果的关系匹配时,我也希望返回该记录。 I've tried to make the OriginalUser model searchable as well, but I'm just stuck at this point.我也尝试过使OriginalUser模型也searchable ,但此时我被困住了。

The documentation isn't clear enough for me to find out how to actually get the results where the relationship columns would match the search value as well.文档不够清楚,我无法找出如何实际获得关系列也与搜索值匹配的结果。 The only thing I've found that says anything about relationships in the model in the official documentation, is:我发现的唯一关于官方文档中模型中关系的内容是:

// You may also add records via relationships...
$user->orders()->searchable();

I've tried this but it doesn't show me the other record.(Of course I changed orders to originalUser() in my code)我试过这个,但它没有向我显示其他记录。(当然我在我的代码中将orders更改为originalUser()

Does anyone know how to search in multiple models within 1 "search" with laravel scout?有谁知道如何使用 laravel scout 在 1 个“搜索”中搜索多个模型?

EDIT: I am not using Algolia, but mysql.编辑:我没有使用 Algolia,而是使用 mysql。

Unfortunately, this doesn't seem to be possible using Scout and the MySQL driver at the moment.不幸的是,目前使用 Scout 和 MySQL 驱动程序似乎无法做到这一点。

Hunting around myself today for a solution to the exact same problem, I found some suggestions that adding relationships via the toSearchAbleArray method on the models you want to search might do it: https://laracasts.com/discuss/channels/laravel/scout-how-to-search-in-relations-and-counts?page=1#reply=329201今天在我身边寻找完全相同问题的解决方案,我发现了一些建议,通过 toSearchAbleArray 方法在您要搜索的模型上添加关系可能会这样做: https ://laracasts.com/discuss/channels/laravel/scout -how-to-search-in-relations-and-counts?page=1#reply=329201

But this didn't work for me, as toSearchableArray handles placing potential search results in an index like Algolia, and the MySql driver doesn't do this.但这对我不起作用,因为 toSearchableArray 处理将潜在搜索结果放置在像 Algolia 这样的索引中,而 MySql 驱动程序不这样做。 After a few hours, I found an issue from 2016 on the repo on the topic, which suggests they haven't got around to implementing this yet: https://github.com/yabhq/laravel-scout-mysql-driver/issues/5几个小时后,我在该主题的 repo 上发现了 2016 年的一个问题,这表明他们还没有开始实施: https : //github.com/yabhq/laravel-scout-mysql-driver/issues /5

If you are interested, I'm working on something similar.如果你有兴趣,我正在做类似的事情。 I also needed to search in related models, so I'm combining together Eloquent-joins project (that make possible doing real join in eloquent model instead of the eager load system used by Laravel) with the mysql scout driver.我还需要在相关模型中进行搜索,因此我将 Eloquent-joins 项目(这使得在 eloquent 模型中进行真正的连接而不是 Laravel 使用的急切加载系统成为可能)与 mysql scout 驱动程序结合在一起。

The only way to search in a related model is doing a where on a joined table, using the full field name (ex. original_users.usernamename).在相关模型中搜索的唯一方法是使用完整字段名称(例如 original_users.usernamename)在连接表上执行 where。 I had to rewrite the scout mysql driver to search the fields to be used even in the specified related model, and to use the join system during the query (and other functionality I need in my project).我不得不重写 scout mysql 驱动程序以搜索即使在指定的相关模型中也要使用的字段,并在查询期间使用连接系统(以及我在项目中需要的其他功能)。

For the eloquent-joins project, I have made a fork that I'm updating to work with Laravel 6 and his new driver system.对于 eloquent-joins 项目,我制作了一个 fork,我正在更新它以使用 Laravel 6 和他的新驱动程序系统。 https://github.com/mix359/eloquent-joins/tree/dev https://github.com/mix359/eloquent-joins/tree/dev

If there's someone interested and I have a bit of time, I can try make a package with the rewritten driver (the structure is different from the original one as it wasn't developed with the idea to be published in a package).如果有人感兴趣并且我有一点时间,我可以尝试用重写的驱动程序制作一个包(结构与原始驱动程序不同,因为它不是为了在包中发布的想法而开发的)。

Byez比耶兹

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM