1

I'm facing problem with laravel ajax i want to define url using laravel action() or route('page.destory') function with id . Ajax code is here

$(".delete").click(function(){
var id = $(this).data("id");
var token = $("meta[name='csrf-token']").attr("content");

$.ajax(
{
    url: "{{action('Admin\Page\PageController@destroy')}}+id",
    method: 'DELETE',
    data: {
        "id": id,
        "_token": token,
    },
    success: function (data){
        alert(data);
    }
});

});

Laravel give me error Missing required parameters for [Route: page.destroy]

in web.php Route::resource('page','PageController);

How can i pass id with that action of destroy. url should be look like http://example.com/admin/page/1. but i do not want to write url manually.Many Thanks in advance.

1 Answer 1

1
url: "{{action('Admin\Page\PageController@destroy')}}+id",

Just guessing but I think that this line is wrong, because you are appending +id string not + variable id

So it's actually produce http://example.com/admin/page/+id which is wrong.

Try to change it like this:

url: "{{ action('Admin\Page\PageController@destroy') }}" + id

Also not sure if you need to add slash before id like this:

url: "{{ action('Admin\Page\PageController@destroy') }}/" + id

Try to open inspector in your browser and look for network tab. Try to click on that delete button and see the last request. You will see there what you are actually posting to server.

In chrome like this: enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

hello buddy, last question which is best way to define script in laravel . in app.js and run dev or on footer of page . what laravel recommend

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.