-4

我尝试使用 Laravel 8 和 axios 在我的 MySQL 数据库中一次插入多行,但我收到此错误在此处输入图像描述

在此处输入图像描述

这是我的数据库表,名为 auroracoins在此处输入图像描述

我获取数据的api是这样的在此处输入图像描述

这是我的 axios 电话

       async insertar(history_records) {
            try {
                console.log(history_records)
                const response = await axios
                    .post("/home/insert", history_records.results)
                    .then(response => {
                        console.log("worked")
                        // this.categories.push(this.categoryToAdd);
                    })
                    .catch(error => {
                        console.log(error);
                    });
            } catch (error) {
                this.request_status = error;
                console.log(error);
            }
        },

那是 web.php 路线在此处输入图像描述

这是存储功能 [![在此处输入图像描述][7]][7]

PD:很抱歉使用图像而不是代码,但我遇到了一些格式问题

    public function store(Request $request)
{
    error_log($request);

    //Create item


    foreach ($request as $history_record) {

        $history = new Auroracoin();
        $history->history_date = $history_record->date;
        $history->rate = $history_record->rate;
    }

    $history->save();

    return $history;
}
4

1 回答 1

2

不要直接循环 $request,它是一个 symfony Request 对象。

要循环您发送的数据,请使用

foreach ($request->all() as $history_record) {
    $history = new Auroracoin();
    $history->history_date = $history_record['date'];
    $history->rate = $history_record['rate'];
}

数据作为 json 对象发送的事件,http 中没有这样的事情。您需要将它们作为关联数组访问$history_record['date']

返回一个数组,其中$request->all()包含随请求发送的所有 POST/GET 数据。

于 2021-07-26T01:37:04.790 回答