PHP Mysql Laravel eloquent因字符串中的反斜杠而出现错误,无法删除反斜杠。

huangapple go评论72阅读模式
英文:

PHP Mysql Laravel eloquent error because of slashes in string, Can't remove slashes

问题

以下是您要翻译的内容:

如何制作这个查询:
Users::whereNotIn('user_id', [$all_users])->update(['disabled' => 1]);
以生成正确的查询:
update `users` set `disabled` = 1, `users`.`updated_at` = '2020-01-03 14:11:09' where `user_id` not in ('642','532','539','588','488','601')

**我无法去掉斜杠。**

字符串构建如下:
$all_users=$all_users.$user_id->id."'',''";

如果我打印$all_users字符串,它会正确打印,如下所示:
echo $all_users;
生成:
'642','532','539','588','488','601'

Users::whereNotIn('user_id', [addslashes($all_users)])->update(['disabled' => 1]);
生成:
update `users` set `disabled` = 1, `users`.`updated_at` = '2020-01-03 13:53:02' where `user_id` not in **('\'642\',\'532\',\'539\',\'588\',\'488\',\'601\'')**

Users::whereNotIn('user_id', [$all_users])->update(['disabled' => 1]);
生成:
update `users` set `disabled` = 1, `users`.`updated_at` = '2020-01-03 14:11:09' where `user_id` not in **('\'642\',\'532\',\'539\',\'588\',\'488\',\'601\'')**

//我还尝试过这个
//Users::whereNotIn('user_id', [$all_users])->update(['disabled' => 1]);
//Users::whereNotIn('user_id', [removeslashes($all_users)])->update(['disabled' => 1]);
//Users::whereNotIn('user_id', [addslashes($all_users)])->update(['disabled' => 1]);
//Users::whereNotIn('user_id', [str_replace("","\\",$all_users)])->update(['disabled' => 1]);

请注意,这是您提供的文本的翻译。如果您需要进一步的帮助或解释,请随时告诉我。

英文:

How to make this:
Users::whereNotIn('user_id', [$all_users])->update(['disabled' => 1]);
to produces this correct query:
update users set disabled = 1, users.updated_at = '2020-01-03 14:11:09' where user_id not in ('642','532','539','588','488','601')

I can't get rid of the slashes.

The string is build like this: $all_users=$all_users.$user_id->id."','";

If I print $all_users string, it prints correctly, see below
echo $all_users;
produces:
'642','532','539','588','488','601'

Users::whereNotIn('user_id', [addslashes($all_users)])->update(['disabled' => 1]);
produces:
update users set disabled = 1, users.updated_at = '2020-01-03 13:53:02' where user_id not in ('\'642\',\'532\',\'539\',\'588\',\'488\',\'601\'')

Users::whereNotIn('user_id', [$all_users])->update(['disabled' => 1]);
produces:
update users set disabled = 1, users.updated_at = '2020-01-03 14:11:09' where user_id not in (''642','532','539','588','488','601'')

//also I tried this
//Users::whereNotIn('user_id', [$all_users])->update(['disabled' => 1]);
//Users::whereNotIn('user_id', [removeslashes($all_users)])->update(['disabled' => 1]);
//Users::whereNotIn('user_id', [addslashes($all_users)])->update(['disabled' => 1]);
//Users::whereNotIn('user_id', [str_replace("","", $all_users)])->update(['disabled' => 1]);

Thanks !

答案1

得分: 1

传递一个数组而不是一个字符串

$all_users[] = $user_id->id;

Users::whereNotIn('user_id', $all_users)->update(['disabled' => 1]);
英文:

Pass an array not a string

$all_users[]=$user_id->id;

Users::whereNotIn('user_id', $all_users)->update(['disabled' => 1]);

huangapple
  • 本文由 发表于 2020年1月3日 22:26:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580272.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定