Laravel 未调用 Scout toSearchableArray()?

Laravel Scout toSearchableArray() not being called?

我使用 Laravel Scout 和 TNTSearch 在我的一个模型(Recipe)上使用搜索功能:teamtnt/laravel-scout-tntsearch-driver.

我想将相同的搜索功能添加到不同的模型(成分)。我试图通过使用 toSearchableArray(). 将搜索结果作为数组返回。为了进行测试,我在我的模型中执行了以下操作。

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;
    }
}

在我的控制器中,我正在尝试这样做:

public function search(Request $request)
{
    $results = Ingredient::search($request->q)->get()->toArray();

    return $results;
}

但是,我仍然以集合的形式取回我的数据。我正在为我的其他模型(配方)使用类似的设置,它会按预期执行 return 结果数组。

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;
    }
}

然后,再次在模型中:

public function search(Request $request)
{
    $resultsrecipes = Recipe::search($request->q)->get()->load('tags', 'ingredients', 'images');

    return $resultsrecipes;
}

这适用于 Recipe 模型,即使没有 load() 功能。我假设我的 Ingredient 模型中的 toSearchableArray() 函数没有被调用。我的问题是如何验证和解决这个问题?

我尝试通过使用 php artisan queue:restart, 尝试刷新和添加记录来重置队列工作器,但似乎没有任何效果。

原来是 php artisan scout:flush doesn't work with TNTSearch,所以我不得不手动删除索引文件并 运行 php artisan scout:import 刷新新模型的可搜索设置。