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.