Mahmoud Khosravi
Mahmoud Khosravi

Reputation: 506

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');
}

Upvotes: 0

Views: 47

Answers (1)

Jignesh Joisar
Jignesh Joisar

Reputation: 15175

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

Upvotes: 1

Related Questions