vendredi 27 avril 2018

How to call a route with Javascript and destroy data from database

I'm working with Laravel 5 and I need to destroy data from database, I've the following code

HTML

<div class="modal fade" id="deletePub" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h6 class="modal-title" id="modalPublicationTitle">Confirm Publication Delete</h6>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body">    
                <div class="row align-items-center">
                    <div class="col-lg-12" align="center">Really, do you want to delete this publication?</div>
                    <a href="#" id="btn-confirmDeletePub" class="btn btn-danger btn-sm" role="button">Yes, Delete</a>
                </div>
            </div>
        </div>
    </div>
</div>

JS

$(document).ready(function(){

  $("#btn-confirmDeletePub").click(function(){

    var publicationId = window.location.href.split("/")[4]

    console.log(publicationId);

    $.ajax({
        type: "DELETE",
        url: "/publications.destroy",
        data: JSON.stringify(publicationId),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
    });

  });

});

web.php

Route::resource('publications','PublicationController);

PublicationController.php

public function destroy($id)
{
    $publication = Publication::find($id);
    $publication->users()->detach($publication->id);
    $publication->topics()->detach($publication->id);
    $publication->authors()->detach($publication->id);

    $publication->details()->delete();

    //$publication->delete();

    Redirect('/users')->with('success', 'Publication deleted correctly.');

}

All the following code should call the btn-confirmDeletePub function of the JS file when I click on the Yes, delete button. The JS button captures the id of the publication to be deleted and will sent to the destroy($id) function of the PublicationController, but for some reason this function it's not called, the error is inside the JS and I'm sure, but I do not know how to solve.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2HBX9Ih
via IFTTT

Aucun commentaire:

Enregistrer un commentaire