You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
I am trying to include categories in my search but not getting any results.
I'm searching the main table "Posts" where there is a hasMany relationship to "Categories" and this has a hasOne relationship to "CategoryList" which has the column "name". This name column is the one that I want to include in the index when updating Posts so posts are searchable via the category name.
Is this possible?
The text was updated successfully, but these errors were encountered:
I think the best workaround is to create a MySQL View. Brent from Spatie has a good article on it.
You can then reference the "MySQL View" model (which is still a regular eloquent model, it just points to a view instead of a table), and now you can search across relationships because the view can be built to join the relationships.
This may help others:
I needed to perform the search on the "main" model (and return it as the result) but obviously use the View model for the actual search query.
I overrode the search() method on the main model so that the query is performed on the search View.
MainModel.php
publicstaticfunctionsearch($query = '', $callback = null) {
returnapp(Builder::class, [
'model' => newMainModelSeachView(), // Just this changed from the parent implementation.'query' => $query,
'callback' => $callback,
'softDelete'=> static::usesSoftDelete() && config('scout.soft_delete', false),
]);
}
Then in the View model I overrode newFromBuilder() in order to return the result as the main model.
MainModelSearchView.php
publicfunctionnewFromBuilder($attributes = [], $connection = NULL) {
return MainModel::find($attributes->id); // Ensure the ID is available in the View table
}
This way I can perform a search like MainModel::search('query') but the View is queried and I still get the result as a collection of MainModels.
Would love to know if there's a better way to do this.
Sign up for freeto subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I am trying to include categories in my search but not getting any results.
I'm searching the main table "Posts" where there is a hasMany relationship to "Categories" and this has a hasOne relationship to "CategoryList" which has the column "name". This name column is the one that I want to include in the index when updating Posts so posts are searchable via the category name.
Is this possible?
The text was updated successfully, but these errors were encountered: