Laravel Ajax 数据中的数组到字符串
Array to String in Laravel Ajax Data
我通过 Ajax 将我的数据发送到控制器,但在后端,我得到 Array to string conversion.
控制器
<?php
public function updateimages(Request $request){
$updateDetails=array(
'image_alt' => $request->get('image_alt')
);
$updateIds=array(
'id' => $request->get('id')
);
$images = DB::table('photos')
->where([$updateIds])
->update([$updateDetails]);
return response()->json(['updated' => $images], 200);
}
Ajax
<script type="text/javascript">
$(document).ready(function() {
$(".SaveImage").on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var idmess= [];
$(".photohe").each(function(){
idmess.push($(this).val());
});
var image_alt= [];
$(".image_alt").each(function(){
image_alt.push($(this).val());
});
console.log(idmess);
console.log(image_alt);
$.ajax({
url: '{{ url('admin/savemulti') }}',
type: 'POST',
dataType: "JSON",
data: {
"id": idmess,
"image_alt": image_alt,
"_method": 'POST',
"_token": "{{ csrf_token() }}",
},
success:function(data) {
var message = "Image Updated successfully!"
$('.saved').append(message);
}
});
});
});
</script>
下面是我的控制器dd获取数据的方式:
array:4 [
"id" => array:2 [
0 => "1045"
1 => "1046"
]
"image_alt" => array:2 [
0 => "mage 1"
1 => "image 2"
]
"_method" => "POST"
"_token" => "SParC5rwYy0KLLhwa0km7fcZvodSqrzhvyqZUqk3"
]
应该更新为:
1045 = mage 1
1046 = image 2
知道如何解决吗?
您不能像在上面的代码中那样进行多次更新。您正在添加要在两个数组中分别更新的 ID 和数据。
您必须像这样更改您的代码。
public function updateimages(Request $request){
$ids = $request->get('id');
$updateImages = $request->get('image_alt');
foreach($ids as $key => $value){
DB::table('photos')
->where('id',$value)
->update(['image_alt'=>$updateImages[$key]]);
}
return DB::table('photos')->whereIn('id',ids)->get();
}
我通过 Ajax 将我的数据发送到控制器,但在后端,我得到 Array to string conversion.
控制器
<?php
public function updateimages(Request $request){
$updateDetails=array(
'image_alt' => $request->get('image_alt')
);
$updateIds=array(
'id' => $request->get('id')
);
$images = DB::table('photos')
->where([$updateIds])
->update([$updateDetails]);
return response()->json(['updated' => $images], 200);
}
Ajax
<script type="text/javascript">
$(document).ready(function() {
$(".SaveImage").on('click', function(e) {
e.preventDefault();
e.stopPropagation();
var idmess= [];
$(".photohe").each(function(){
idmess.push($(this).val());
});
var image_alt= [];
$(".image_alt").each(function(){
image_alt.push($(this).val());
});
console.log(idmess);
console.log(image_alt);
$.ajax({
url: '{{ url('admin/savemulti') }}',
type: 'POST',
dataType: "JSON",
data: {
"id": idmess,
"image_alt": image_alt,
"_method": 'POST',
"_token": "{{ csrf_token() }}",
},
success:function(data) {
var message = "Image Updated successfully!"
$('.saved').append(message);
}
});
});
});
</script>
下面是我的控制器dd获取数据的方式:
array:4 [
"id" => array:2 [
0 => "1045"
1 => "1046"
]
"image_alt" => array:2 [
0 => "mage 1"
1 => "image 2"
]
"_method" => "POST"
"_token" => "SParC5rwYy0KLLhwa0km7fcZvodSqrzhvyqZUqk3"
]
应该更新为:
1045 = mage 1
1046 = image 2
知道如何解决吗?
您不能像在上面的代码中那样进行多次更新。您正在添加要在两个数组中分别更新的 ID 和数据。
您必须像这样更改您的代码。
public function updateimages(Request $request){
$ids = $request->get('id');
$updateImages = $request->get('image_alt');
foreach($ids as $key => $value){
DB::table('photos')
->where('id',$value)
->update(['image_alt'=>$updateImages[$key]]);
}
return DB::table('photos')->whereIn('id',ids)->get();
}