我试图在我的数据库中一次插入许多行,但我得到了一个错误

t1rydlwq  于 2021-09-29  发布在  Java
关注(0)|答案(1)|浏览(281)

关闭。这个问题需要更加关注。它目前不接受答案。
**想改进这个问题吗?**编辑这篇文章,更新这个问题,使它只关注一个问题。

昨天关门了。
改进这个问题
我试图用laravel 8和axios一次在mysql数据库中插入许多行,但我遇到了这个错误


这是我的db表,叫做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]
抱歉用图像代替代码,但是我有一些格式化问题

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;
}
iqih9akk

iqih9akk1#

不要直接循环$request,它是一个symfony请求对象。
要循环发送的数据,请使用

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数据。

相关问题