Home » Php » php – How we fetch data on Bootstrap modal box using Laravel MySQL

php – How we fetch data on Bootstrap modal box using Laravel MySQL

Posted by: admin February 25, 2020 Leave a comment

Questions:

I work Laravel application we display data on datatable. When we update data using Bootstrap modal box using foreach then only fetch last data in the table.

Button where we link:

@foreach($patients as $patient)<br>
    <li>
        <button type="button"
            class="btn btn-primary btn-sm"
            data-toggle="modal"
            data-target="#myModal{{$patient->patient_id}}">Update Fee</button>
    </li>
@endforeach

Modal box:

<div id="myModal{{$patient->patient_id}}" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title"><span class="glyphicon glyphicon-edit"></span>&nbsp;Edit Fee</h4>
            </div>
            <div class="modal-body">
                <form>
                    <div class="form-group">
                        <label> Total Fee</label>
                        <input type="text" name="fee" class="form-control" readonly value="{{$patient->Fee->Fee}}" />
                        <input type="hidden" name="id" class="form-control">
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
How to&Answers:

Just insert your modal inside @foreach directive (your loop) and that should work on your current code.

@foreach($patients as $patient)<br>
<li>
    <button type="button"
            class="btn btn-primary btn-sm"
            data-toggle="modal"
            data-target="#myModal{{$patient->patient_id}}">Update Fee</button>
</li>

<div id="myModal{{$patient->patient_id}}" class="modal fade" role="dialog">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title"><span class="glyphicon glyphicon-edit"></span>&nbsp;Edit Fee</h4>
        </div>
        <div class="modal-body">
            <form>
                <div class="form-group">
                    <label> Total Fee</label>
                    <input type="text" name="fee" class="form-control" readonly value="{{$patient->Fee->Fee}}" />
                    <input type="hidden" name="id" class="form-control">
                </div>
            </form>
        </div>
    </div>
</div>
@endforeach