lundi 23 septembre 2019

Laravel AJAX getting all records in one JSON request

i am trying to edit record in my database using ajax, my code is working fine, but i have to mention each column by name, how i can get same result without typing all columns name.

Edit Controller: i am using columns name [efirst,esecond etc] i want to pass everything from database without mentioning name

public function edit($id)
    {
        $teacher = Teacher::find($id);
        return response()->json([
            'status' => 'success',
            'id' => $teacher->id,
            'efirst' => $teacher->efirst,
              'esecond' => $teacher->esecond,
        ]);
    }

Edit.js:

jQuery(document).ready(function($)  {
    $(".table-container").on("click touchstart", ".edit-btn", function () {
        $.ajax({
            type: "GET",
            url: "lists/" + $(this).attr("value") + "/edit",
            dataType: 'json',
            headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
            beforeSend: function() {
            $('#esecond-not-found').remove();
            },
            success: function (data) {
                $("#update-id").val(data['id']);
                $("#update-efirst").val(data['efirst']);
                $("#update-esecond").val(data['esecond']);
                $('#update-form').show();
            },
        });
    });
});

View:

<form method="post" id="update-form">

 <input type="hidden" name="id" id="update-id">
  <div class="">
    <label for="efirst">efirst</label>
       <input type="text" class="form-control" name="efirst" id="update-efirst">   
         <label for="esecond">esecond body</label>
           <textarea name="esecond" class="form-control" id="update-esecond" rows="6"></textarea>
 </div>
    <div class="">
       <button type="submit" class="btn btn-success"  id="update-submit">Update</button>
         <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
</form>


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire