arrays - Laravel Scout toSearchableArray() 没有被调用?

标签 arrays laravel collections toarray laravel-scout

我使用 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;
    }
}

在我的 Controller 中,我正在尝试这样做:

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

    return $results;
}

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

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

即使没有 load() 函数,这也适用于 Recipe 模型。我假设我的 Ingredient 模型中的 toSearchableArray() 函数没有被调用。我的问题是如何验证并修复此问题?

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

最佳答案

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

关于arrays - Laravel Scout toSearchableArray() 没有被调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50679645/

相关文章:

c++ - 打印数组时的垃圾值

arrays - 访问该数组的正确语法

css - 将自定义字体添加到 laravel 应用程序

php - 如何在 Laravel 中为模型命名空间以免与现有类发生冲突?

.net - 对象集合的统一和依赖

java - 为什么我们有不可变的空映射?

arrays - 绕过字符串数组作为参数并在 PostgreSQL 中使用动态查询

python - 在 Numpy 中为值对生成矩阵

php - Laravel,使用 session 将输入变量从页面传递到其他页面

c# - LINQ 可以用于在单个操作中搜索多个列表吗?