0

I am trying to update a restaurant menu via form. firstly I take all contents of the menu category and put them via foreach in a input value so they are all editable and pre setup for editing.

now the big problem is whenever I completely try to edit the form, it only edits the last row because that's the last received and thus ignoring the rest. How would I make the form submit all requested items from the foreach?

making a form for each item would be a big no no because then there would be an button for each row which hinders the productivity

Blade view:

                    <form action="test" method="head">
                        @foreach ($Voorgerecht as $item)
                        @csrf
                        <tr>
                            <td>
                                <input type="text" name="id" value="{{$item->id}}">
                            </td>
                            <td>
                                <input type="text" name="name" value="{{$item->product_name}}">
                            </td>

                            <td>
                                <input type="text" name="price" value="{{$item->product_price}}">
                            </td>
                            <td>
                                <input type="submit" value="edit">
                            </td>
                            @endforeach 
                        </tr>
                    </form>

Controller:

public function editsubmit(Request $req) {

        update::where('id',$req->id)
        ->update(['product_name'=>$req->name]);

        return redirect('/admin');
}
Ankit Jindal
  • 3,672
  • 3
  • 25
  • 37
Rick W
  • 75
  • 7
  • Names are unique, so if there are multiple inputs using the same name, then it will only pass in the last one. You'll want to use an array name as in CBroe's link – aynber Jan 27 '21 at 13:12
  • You also need to move the closing row tag `` inside the `foreach` to avoid creating invalid markup (if you open a row in a loop, you need to close it there too). – El_Vanja Jan 27 '21 at 13:26
  • I moved the but the thing is I have them now in array but it still puts them in a sql ending on WHERE id = 1 thus putting all arrays in one – Rick W Jan 27 '21 at 13:41

2 Answers2

1

If you really want to do this all in one go you can name the inputs in array form as a container for all the records, something like records[1][product_name]:

<form ...>
    @csrf
    @foreach ($Voorgerecht as $item)
    <tr>
        ...
        <td>
            <input type="text" name="records[{{ $item->id }}][product_name]" value="...">
        </td>
        <td>
            <input type="text" name="records[{{ $item->id }}][product_price]" value="{{ $item->product_price }}">
        </td>
        ...
    </tr>
    @endforeach
</form>

Then in the Controller

... do validation ...

foreach ($request->input('records', []) as $id => $record) {
    Model::findOrFail($id)->update($record);
}
lagbox
  • 48,571
  • 8
  • 72
  • 83
0

You can change the form fields so they are posted as an array:

<input type="text" name="id[{{ $loop->index }}]" value="{{$item->id}}">

i added the $loop->index so all fields are combined.

Gert B.
  • 2,282
  • 18
  • 21
  • okay so that seems to work almost now am getting this: SQL: update `Products` set `product_name` = ? where `id` = 3 – Rick W Jan 27 '21 at 13:30
  • jokes on me i forgot to also edit the name.. the only problem now is that it edits only one row having id 1... so all items are now in array but are put into the first row – Rick W Jan 27 '21 at 13:37