Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mwpdx's avatar
Level 1

Bootstrap 4 foreach table rows & and individual edit modals

Hey everyone... the title sounds kinda weird, but that's the best I could come up with :p

Basically, I am running Bootstrap 4 beta and Laravel 5.5.0, pushing a collection through a foreach loop in a table. Each table row has unique data, and each row has an edit button. When hitting the edit button, the Bootstrap modal should pop up with the form fields populated with the data.

Instead, the background goes dark (expected behavior), but the modal never loads. FF and Chrome's js console shows nothing in terms of errors. I'd imagine that I need to pull in data via ajax GET to populate it, but I'm stumped on how to go about that...

Here's the button loading on each row:

<button class="btn btn-warning btn-sm" id="showEditModal" data-toggle="modal" data-id="{{ $contact->id }}" data-target="#editModal"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> <strong>Edit</button>

I have a GET route and controller that does pull data in....

Route::get('/contact/edit/{id}', 'ContactController@edit')->name('contactedit');

And I am returning a json object from the that controller and method:

public function edit($id) { $get_contact = new Contact; $contact = $get_contact::find($id); return [ 'firstname' => $contact->first_name, 'lastname' => $contact->surname, 'email' => $contact->email, 'phone' => $contact->phone, 'contact_id' => $contact->id ]; }

Finally, just for the sake of trying to get this thing to load, I am at the moment working with an empty modal...

https://pastebin.com/Cwb3Rm1z

Console shows the json object returns just fine. So... where do I go from where?

Thanks.

0 likes
4 replies
RamjithAp's avatar

The simple & best way to do it on the page load itself load all the edit modals inside foreach loop like below and give unique ID for the modals.

<table>
<tr>
<td>Name</td>
<td<Edit</td>
</tr>
@foreach($contacts as $contact)
<tr>
<td>{{$contact->name}}</td>
<td>
<button class="btn btn-warning btn-sm" data-toggle="modal" data-id="{{ $contact->id }}" data-target="#editModal{{$contact->id}}"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> <strong>Edit</strong> </button>
</td>
</tr>
<!-- add edit modal -->
<div id="editModal{{$contact->id}}" class="modal fade" role="dialog">
        <div class="modal-dialog">
            <!-- Modal content-->
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal">×</button>
                    <h4 class="modal-title">Edit {{$contact->name}}</h4><br>
                </div>
                <div class="modal-body">
                    {{ Form::open(array('url' => 'http://yourdomain/updatecontact/'.$contact->id)) }}
                   <input type="text" name="first_name" value="{{$contact->first_name}}">
                  <input type="text" name="email" value="{{$contact->email}}">
                  <input type="text" name="phone" value="{{$contact->phone}}">
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-default" >Save</button>
                 {{ Form::close()}}
                </div>
            </div>
        </div>
    </div>
<!-- ./add edit modal -->

@endforeach 
</table>

Make sure you have included the bootstrap library for enabling modal popups.

SyedAbuthahir's avatar

Return a html form instead of Json Object

Example

Blade view for form (contact/form-fields.blade.php)

<div class="form-group">
    <label for="exampleInputEmail1">Email address</label>
    <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter email" value="{{ $contact->email }}">
  </div>
  <div class="form-group">
    <label for="firstname">First Name</label>
    <input type="text" class="form-control" id="irstname" placeholder="Password"  value="{{ $contact->first_name }}">
  </div>

Controller Method

public function edit($id) {
    $contact = Contact::find($id);
    return view('contact.form-fields.php',compact('contact'));
}

Use Bootstrap Model Events to load form-fields

$('#exampleModal').on('show.bs.modal', function (event) {
  var button = $(event.relatedTarget) // Button that triggered the modal
  var recipient = button.data('whatever') // Extract info from data-* attributes
  // If necessary, you could initiate an AJAX request here (and then do the updating in a callback).
})

Code samples are just for demo purpose.

zion's avatar

Considering you have the json object already, use the ajax success function to set the values.

success: function(data) {
    $("TheFieldName").val(data.TheFieldName);

    // Toggle the modal
    $("#editModal").modal('toggle');
}
1 like

Please or to participate in this conversation.