Reputation: 885
I have working search functionality on one of my models (Recipe) using Laravel Scout and TNTSearch: teamtnt/laravel-scout-tntsearch-driver.
I want to add the same search functionality to a different model (Ingredient). I'm attempting to get search results back as an array by using toSearchableArray().
For the sake of testing, I did the following in my model.
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Ingredient extends Model
{
use Searchable;
public $asYouType = true;
public function recipes()
{
return $this->belongsToMany('App\Recipe');
}
public function toSearchableArray()
{
$array = $this->toArray();
return $array;
}
}
In my controller I am trying this:
public function search(Request $request)
{
$results = Ingredient::search($request->q)->get()->toArray();
return $results;
}
However, I am still getting my data back as a collection. I am using a similar set up for my other model (Recipe) which does return an array of results, as expected.
namespace App;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Recipe extends Model
{
use Searchable;
public $asYouType = true;
public function ingredients()
{
return $this->belongsToMany('App\Ingredient');
}
public function toSearchableArray()
{
$array = $this->toArray();
return $array;
}
}
Then, again, in the model:
public function search(Request $request)
{
$resultsrecipes = Recipe::search($request->q)->get()->load('tags', 'ingredients', 'images');
return $resultsrecipes;
}
This works for the Recipe model, even without the load()
function. I assume that the toSearchableArray()
function in my Ingredient model is not being called. My question is how can I verify and fix this?
I tried resetting the queue worker by using php artisan queue:restart,
tried flushing and adding the records but nothing seems to work.
Upvotes: 3
Views: 3809
Reputation: 885
Turns out php artisan scout:flush doesn't work with TNTSearch, so I had to remove the index file manually and run php artisan scout:import to refresh the Searchable settings for the new model.
Upvotes: 5