Laravel scout search in relationships
Asked Answered
R

2

7

I can't seem to figure out how to search in 2 tables within 1 query in Laravel scout. 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.

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)

Does anyone know how to search in multiple models within 1 "search" with laravel scout?

EDIT: I am not using Algolia, but mysql.

Raouf answered 16/12, 2019 at 13:53 Comment(0)
D
3

Unfortunately, this doesn't seem to be possible using Scout and the MySQL driver at the moment.

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

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. 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

Dozer answered 20/2, 2020 at 12:9 Comment(0)
R
0

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.

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). 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).

For the eloquent-joins project, I have made a fork that I'm updating to work with Laravel 6 and his new driver system. 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

Ryan answered 5/3, 2020 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.