Skip to content
Advertisement

How to save data?

My data is like this

first

After re-saving the data is saved two double.

second

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

Advertisement

Answer

you need to used updateOrCreate method instead of 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');
}

for more information read this article https://eloquentbyexample.com/course/lesson/lesson-7-creating-and-updating

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement