gpt4 book ai didi

php - 使用 Laravel Fluent Query Builder,有没有办法根据 2 个子字符串搜索文本?

转载 作者:行者123 更新时间:2023-11-29 12:23:30 25 4
gpt4 key购买 nike

假设我的数据库中有如下所示的字符串:

The quick brown fox 0 jumps over the lazy dog 0.
The quick brown fox 0 jumps over the lazy dog 1.
The quick brown fox 0 jumps over the lazy dog 2.
The quick brown fox 1 jumps over the lazy dog 0.
The quick brown fox 1 jumps over the lazy dog 1.
The quick brown fox 1 jumps over the lazy dog 2.

有没有办法使用 Laravel Fluent Query Builder 搜索 2 个子字符串?

假设我正在寻找“fox 0”和“dog”。预期结果是:

The quick brown fox 0 jumps over the lazy dog 0.
The quick brown fox 0 jumps over the lazy dog 1.
The quick brown fox 0 jumps over the lazy dog 2.

这是我目前拥有的:

$searchStr = ["fox 0", "dog"];
$query->where('text', '=', $searchStr);

最佳答案

为您提供这些结果的 SQL 查询条件是 where text like '%fox 0%' and text like '%dog%'

翻译成流畅的查询是:

$query->where('text', 'like', '%fox 0%')->where('text', 'like', '%dog%');

或者,与你的数组保持一致:

$searchStr = ["fox 0", "dog"];
foreach($searchStr as $str) {
$query->where('text', 'like', '%'.$str.'%');
}

关于php - 使用 Laravel Fluent Query Builder,有没有办法根据 2 个子字符串搜索文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28687430/

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