【问题标题】:How to save data?如何保存数据?
【发布时间】:2021-04-13 02:38:38
【问题描述】:

我的数据是这样的

重新保存后数据保存两次双倍。

public function store(Request $request)
{
    foreach ($request->name as $key => $name) {
        Setting::firstOrCreate(
            ['name' => $name],
            ['link' => $request->link[$key]]
        );
    }
    return redirect()->route('settings.index');
}

【问题讨论】:

  • 请分享更多细节,以及您的调试尝试

标签: php ajax laravel eloquent


【解决方案1】:

你需要使用updateOrCreate方法而不是firstOrCreate

public function store(Request $request)
{
    foreach ($request->name as $key => $name) {
        Setting::updateOrCreate(
            ['name' => $name],
            ['link' => $request->link[$key]]
        );
    }
    return redirect()->route('settings.index');
}

更多信息请阅读这篇文章https://eloquentbyexample.com/course/lesson/lesson-7-creating-and-updating

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2018-06-05
    • 2017-11-17
    • 2023-03-29
    • 2010-11-26
    • 2017-10-14
    • 2016-04-02
    • 2019-04-24
    • 2012-02-27
    相关资源
    最近更新 更多