英文:
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]);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论