我如何渴望加载Laravel Scout?
这是我的问题:
$results = Submission::search($query, function ($meilisearch, $query, $options) use ($request) {
$resultsFilter = '';
if ($subchan = $request->input('subchan')) {
$resultsFilter = appendToFilter($resultsFilter, 'subchan = ' . $subchan);
}
if ($incl_nsfw = $request->input('incl_nsfw')) {
if ($incl_nsfw != 1) {
$resultsFilter = appendToFilter($resultsFilter, 'nsfw != 1');
}
} else {
$resultsFilter = appendToFilter($resultsFilter, 'nsfw != 1');
}
if ($time = $request->input('time')) {
$nowTimestamp = strtotime(CarbonCarbon::now());
$timeString = $time;
switch ($time) {
case "hour":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 3600);
break;
case "day":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 86400);
break;
case "week":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 604800);
break;
case "month":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 2592000);
break;
case "year":
$resultsFilter = appendToFilter($resultsFilter, 'created_at_ts > ' . $nowTimestamp - 31536000);
break;
}
}
if ($resultsFilter != '') {
$options['filters'] = $resultsFilter;
}
return $meilisearch->search($query, $options);
})
->paginate(15)
->withQueryString();
但由于这使用了Scout生成器,传统的热切加载关系是不可能的。作为参考,我使用的是美丽搜索。
您也可以使用query((方法并传递回调
Submission::search($query)->query(fn(Builder $query) => $query->with('relationship'))->get()
搜索结果将包括热切加载的关系
想明白了!它必须在查询之后完成,就像这样:
$results->load('owner', 'savedSubmissions');
与常规的热切加载相反,因为Scout使用自己的生成器。