samedi 28 avril 2018

How do I know if my route is reached?

I'm working with Laravel 5 and I'm trying to call the destroy function inside my Controller

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">    
                <form id="msform" action="" method="post">
                    
                        
                    <div class="row align-items-center">
                        <div class="col-lg-12" align="center">Really, do you want to delete this publication?</div>
                       <button type="button" class="btn btn-danger btn-sm" id="btn-confirmDeletePub">Yes, Delete </button> 
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

JS

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

        $.ajaxSetup({
            headers: {
              'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')  
            }
        });

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

        console.log(publicationId);

        $.ajax({
            type: "POST",
            url: "/publications/" + publicationId,
            data: {_method: 'delete'},
            //contentType: "application/json; charset=utf-8",
            //dataType: "json",
        });
    });
});

web.php

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

PublicationController.php

/**
 * Remove the specified resource from storage.
 *
 * @param  int $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    $publication = Publication::find($id);
    dd($publication);
    $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.');

}

the html code calls 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 I don't have the certainty that the method is called. I tried to put a dd($ publication) to print on the Google Chrome's console, but nothing to do. The error I get is: POST http://localhost:8000/publications/4 500 (Internal Server Error).



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire