我正在通过 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);
}
阿贾克斯
<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
知道如何解决吗?