gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 01:55:55 25 4
gpt4 key购买 nike

我使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com