Laravel 雄辩替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29803476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent REPLACE
提问by xonorageous
I'm creating a database migration script in a Laravel 4 command.
我正在 Laravel 4 命令中创建数据库迁移脚本。
All the database migration process is fine but I'm having trouble changing user prefixes: I need to change USER-JOHN-JB TO USER-JOHN-OD, all the user suffixes need to be changes from '-JB' to '-OD'.
所有数据库迁移过程都很好,但我在更改用户前缀时遇到问题:我需要将 USER-JOHN-JB 更改为 USER-JOHN-OD,所有用户后缀都需要从“-JB”更改为“-OD” '。
The current line of code I have is:
我当前的代码行是:
DB::table('users')->update(['username' => DB::raw("REPLACE(username, '-".$suffix."', '-".$thisSuffix."')")]);
If I run this code in the REPL it works fine however it doesn't work in the command.
如果我在 REPL 中运行此代码,它可以正常工作,但在命令中不起作用。
Looking at the logs the generated SQL query is:
查看日志生成的 SQL 查询是:
update `users` set `username` = REPLACE(username, "-JB
", "-OD")
For some reason a line break is being inserted into the query. I'm not sure if that has anything to do with the problem.
由于某种原因,一个换行符被插入到查询中。我不确定这是否与问题有关。
Does anybody know how to fix this? Any help is much appreciated.
有谁知道如何解决这个问题?任何帮助深表感谢。
Thanks in advance.
提前致谢。
回答by xonorageous
As Ben Harold pointed out, the problem came from $suffix containing a line break. Adding trim() fixed the problem.
正如 Ben Harold 指出的那样,问题来自包含换行符的 $suffix。添加 trim() 解决了这个问题。