Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel scout search in relationships

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.

like image 643
Loko Avatar asked Dec 16 '19 13:12

Loko


People also ask

What is scout in Laravel?

Laravel Scout renders a simple, driver-based solution for implementing a full-text search to your Eloquent models. Utilizing model observers, Scout automatically retains your search indexes in sync with your Eloquent records.

How do I make a model searchable in Laravel?

Finally, add the Laravel\Scout\Searchable trait to the model you would like to make searchable. This trait will register a model observer to keep the model in sync with your search driver: While not strictly required to use Scout, you should strongly consider configuring a queue driver before using the library.

How to implement Instant Search in Laravel?

After the app is created, get into the app’s folder: Next, you have to place the database name, username, and password into the .env configuration file. Now, we need to install the laravel scout and algolia search dependencies using the composer tool; these packages are imperative in order to implement instant search work in laravel.

What is Laravel 8 full-text search?

Linux Vs Windows - Which is Better? Laravel 8 scout full-text search tutorial; In a web application, a Full-text search feature is exorbitantly helpful for site users to traverse through content-rich web and mobile applications.


1 Answers

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

like image 125
Orchis Avatar answered Oct 03 '22 04:10

Orchis