vendredi 10 janvier 2020

Laravel, how to update data from dynamic inputs?

how I'm possible to update data from dynamic form using dynamic inputs? I have tryed first to delete all data with the id from table and then insert data like on creation, but it always stuck on 1 row from dynamic form.

Like: Name | Page | Action Test1 | 22 | Add more Test2 | 24 | Remove Test3 | 23 | Remove

When i save it, it saves only row with "Test1".

Update controller:

public function update(Request $request, Vote $vote)
{
    $request->validate([
        'item.*.name' => 'required|string|max:191',
        'item.*.page' => 'nullable|string',
    ]);

    if(!$vote->vote_options->isEmpty()) {
        VoteOption::where('vote_id', $vote->id)->delete();
        //$vote->vote_options()->delete();
    }

    foreach($request->item as $key => $value) {
        $vote->vote_options()->create($value);

        return redirect()->route('admin.votes.view');
    }

    return redirect()->route('admin.votes.options.edit', $vote->id);
}

Thanks for any ideas, probably this way how I'm doing this is bad :/



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire