考虑这段代码:
$search = "John";
return User::whereRaw("(CONCAT(users.firstName, ' ', users.lastName) like '%$search%')")
->take(20)
->toSql();
这会生成以下 SQL 代码:
select * from `users`
where (CONCAT(users.firstName, ' ', users.lastName) like '%John%')
limit 20
如果我将上面的 toSql()
更改为 get()
,那么 Laravel 将返回相关记录。
到目前为止,一切顺利。
然后,如果我将 $search
的值更改为 John%Anthony
并再次运行上述代码,Laravel 会生成以下 SQL:
select * from `users`
where (CONCAT(users.firstName, ' ', users.lastName) like '%John%Anthony%')
limit 20
如果我直接在 MySQL 中运行此查询,它可以正常工作并返回相关行。然而,Laravel (v5.2) 似乎不喜欢它,因为当我将 toSql()
更改为 get()
时,我收到此错误:
UnexpectedValueException in Response.php line 397:
The Response content must be a string or object implementing __toString(), "boolean" given.
最佳答案
事实证明,错误是由 MySQL 表中的 BLOB
字段中的数据引起的。它没有出现在第一个记录集中,因为它们的 BLOB
字段不包含引号。但是,对于第二个记录集,BLOB
字段在其中一个记录中包含双引号,从而导致抛出该错误。
关于php - MySQL 看起来不错,但 Laravel 不喜欢它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36314331/