gpt4 book ai didi

laravel - 在 Scout 中使用更复杂的 where 子句

转载 作者:行者123 更新时间:2023-12-05 07:26:20 25 4
gpt4 key购买 nike

我是 Laravel Scout 的快乐用户。

现在我想扩展我的搜索:

        $data
= new UserOverviewResourceCollection(User::search($searchphrase)
->currentStatus('active')->orderBy('lastname', 'asc')
->orderBy('firstname', 'asc')
->paginate(config('pagination.length')));

currentStatus 来自https://github.com/spatie/laravel-model-status .

现在我收到回复,不支持 currentStatus。我认为在 User::search 从 scout 返回后过滤结果是个好主意?

另一个想法:我想添加更复杂的 where 子句:

->where([
[
'starts_on',
'<',
Carbon::now()->toDateTimeString(),
],
[
'ends_on',
'>',
Carbon::now()->toDateTimeString(),
],
])

也许你有更好的主意?

最佳答案

我也确实需要类似的功能,但恐怕目前还不可能 (Laravel 8)

根据文档; Laravel\Scout\Builder(这是 search() 返回的)只有 where 方法的真正基本实现,并且只能准确处理2 个参数。这是侦察包中的代码:

/**
* Add a constraint to the search query.
*
* @param string $field
* @param mixed $value
* @return $this
*/
public function where($field, $value)
{
$this->wheres[$field] = $value;

return $this;
}

参见 https://laravel.com/docs/8.x/scout#where-clauses

search() 放在所有常规的 Eloquent 构建器方法后面也不会起作用,因为 search 将只是模型的静态函数。

希望这会在未来得到改进。

关于laravel - 在 Scout 中使用更复杂的 where 子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54399635/

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