3

I am trying to update a specific row using modal. However I don't have any idea how to pass the value of the row id to the route parameter.

Here's the update form.

           <form action="{{route('subcategory.update', 'idhere')}}" method="POST">
                @method('PATCH')
                @csrf
                    <div class="modal-body">
                        <label for="editname">New sub-category name:</label>
                        <input type="text" name="editname" id="editname" class="form-control">
                        <input type="text" name="editid" id="editid" value="" hidden>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                        <button class="btn btn-primary" type="submit">Save changes</button>
                    </div>
            </form>

Edit button

<button class="btn btn-secondary btn-sm" data-myid="{{$item->id}}" 
        data-mytitle ="{{$item->name}}"
        data-target="#editsub" data-toggle="modal">Edit</button>

And here's the controller

    public function update(Request $request, Subcategory $subcategory)
    {
    // return $request;

    Subcategory::where('id',$request->editid)->update([
        'name' => $request->editname
    ]);

    return back();
    }
kwestionable
  • 496
  • 2
  • 8
  • 23
  • You can pass through controller, can i see your form load controller function ? – Casper Sep 09 '19 at 04:12
  • I got it working just now. I placed the `modal` inside the `foreach` function. Is that okay in terms of performance??? – kwestionable Sep 09 '19 at 04:14
  • You can update your question then others will be able to answer your question easily. – Casper Sep 09 '19 at 04:16
  • Duplicating the modal html for every row is fine if you only have a few rows. If you have many, or the number of rows will grow over time, you will soon have a huge html page, slow to load and use. A better option is to have just 1 modal, and use Javascript to update it when a button is clicked. In fact it looks like you were trying this already, you have a `data-myid` on your button. This is a very common approach and there are many examples here on SO about it - [here's one](https://stackoverflow.com/questions/22394696/load-modal-with-custom-data-id). – Don't Panic Sep 09 '19 at 08:34

3 Answers3

0

When you pass an id here and if your model name is subcategory.php you will get the db record in your controller.

 <form action="{{route('subcategory.update', $subcategory )}}" method="POST">

Or you can do following too

<form action="{{route('subcategory.update', $subcategory->id )}}" method="POST">

Your route should have {subcategory} in it for eager loading. After that you can do following in your code.

public function update(Request $request, Subcategory $subcategory)
{

    //variable $subcategory has the db record
    $subcategory->update(['name' => $request->only('editname')]);

    return back();
}
Mike Ross
  • 2,942
  • 5
  • 49
  • 101
0

If I understand it correctly, you can store the item id in

<input type="text" name="editid" id="editid" value="{$item->id}" hidden> 

And since you are using POST request, you can easily get the item id from the request.

Like the one in your post:

$request->input('editid');  
fmsthird
  • 1,745
  • 2
  • 17
  • 34
0

Please try first to pass id in input box hidden field

PHP Geek
  • 3,949
  • 1
  • 16
  • 32