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

标签 php mysql laravel fluent

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

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/

相关文章:

mysql - 如何为 MySQL 中的每一行获取可变大小的子字符串?

php - WAMP Laravel - 从一个本地站点向另一个本地站点发送 API 请求会混淆环境变量

php - move_uploaded_file() 返回 false

php - 相对日期格式始终输出 "3 hours ago"

php - 使用其他网络帐户登录帐户实现

php - 在自定义编码论坛的正确标题下显示类别

mysql - 如何使用 Codeigniter 和 MySQL 更新 TIMESTAMP?

php - laravel dompdf 获取base64格式

laravel - 删除 Zizaco/entrust 中的角色时出现问题

PHP按位存储星期几