反应:仅发送数组

问题描述

我正在使用 Reactaxios一个简单的列表中获取和发布数据。

在控制台中的 post 上,我有以下内容

{data: "",status: 200,statusText: "OK",headers: {…},config: {…},…}
config: {url: "http://localhost:8000/api/social/reorder/",method: "post",data: "{"order":[{"id":2,"order":1},{"id":1,"order":2},{"id":3,"order":3}]}",transformRequest: Array(1),…}
data: ""
headers: {cache-control: "no-cache,private",connection: "close",content-type: "text/html; charset=UTF-8",date: "Thu,22 Apr 2021 16:48:09 GMT,Thu,22 Apr 2021 16:48:09 GMT",host: "localhost:8000",…}
request: XMLHttpRequest {readyState: 4,timeout: 0,withCredentials: false,upload: XMLHttpRequestUpload,onreadystatechange: ƒ,…}
status: 200
statusText: "OK"
__proto__: Object

意思是新推送的数据是:

data: "{"order":[{"id":2,"order":3}]}"

我想问题是我只需要没有 order 的数组才能让数据库更新信息,就像这样:

[{"id":2,"order":3}]

所以我表的每个字段都会收到一个对应于它们的 id 的新订单。

目前,数据已推送,正如您在控制台中看到的那样,但未在数据表中注册。所以发送的数据肯定是错误的,我怀疑是order

我的功能

function handleChangeOrder() {

  const items = Array.from(list);
  let newSequence = [];
  items.forEach((item,index) => {
      newSequence.push({ id: item.id,order: index + 1 });
  });
  axiospost(newSequence);  
  
}


async function axiospost(order){  

try {
  const response = await  axios.post('http://localhost:8000/api/social/reorder/',{order});
  return response.data;

} catch (error){console.error("Not able");}

}

我的后端控制器是否有帮助:

public function reorder(Request $request)
    {
        $this->validate($request,['order'=>'required']);
        $order = $request->input('order');

        DB::transaction(function () use ($order){
            foreach ($order as $value){
                Social::find($value['id'])->update(['order'=>$value['order']]);
            }
        });

    }

解决方法

我的控制器语法有问题,因为数据正在传递到数据库,但语法有问题。

更新时,我应该使用 where 而不是 find,如下所示:

 DB::transaction(function () use ($order){
            foreach ($order as $value){                
                Social::where('id',$value['id'])->update(['order' =>$value['order']]);
            }
        });

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...