[1,2] 的 whereIn 在一次查询中更新产品的多行。 ajax: $('-6ren">
gpt4 book ai didi

mysql - Laravel 使用 whereIn 更新多行

转载 作者:行者123 更新时间:2023-11-30 21:45:18 26 4
gpt4 key购买 nike

我通过 ajax 在字符串中有 id,

例子:

"1,2"

在我的 Controller 中,我想使用带有 id => [1,2]whereIn 在一次查询中更新产品的多行。

ajax:

$('#add_product').click(function() { 
var id = $('#get_value').val();
$.ajax({
type: 'POST',
url: "{{action('ProductController@updateProduct')}}",
data: {
id: id
},
success: function(data){
alert("success");
}
});
});

Controller :

public function updateProduct(Request $request){
$test_value = collect($request->id);
$updateProduct = Product::whereIn('id',$test_value)
->update(['title' => 'My title']);
}

但是我在网络中有这个错误:

SQLSTATE[22007]:无效的日期时间格式:1292 截断不正确的 DOUBLE 值:'1,2'(SQL:更新“产品”设置“标题”=“我的标题”,其中“id”在(1, 2))

最佳答案

$test_value 中,您得到值 "1,2"作为一个字符串,所以你必须首先使用 explode() 函数(explode() 函数做的是 here ),

$ids = explode(',',$test_value);

然后传递这个$ids进入你的功能。

public function updateProduct(Request $request){
$test_value = $request->id;
$ids = explode(',',$test_value);
$updateProduct = Product::whereIn('id',$ids)
->update(['title' => 'My title']);
}

关于mysql - Laravel 使用 whereIn 更新多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49752245/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com