search

Home  >  Q&A  >  body text

将Extract an ID from a URL and transmit it to the Laravel controller in VueJS

I'm trying to submit a form in Vue, my route is

http://cricketstats.test/en/dashboard/players/z2d4ca09a7/india/941/runs

Here 941 is the player ID. I want this id to be passed to the Laravel controller along with the rest of the form data as well.

Is there a way to get this id from the url in vue and pass it to the laravel controller?

P粉754477325P粉754477325308 days ago410

reply all(1)I'll reply

  • P粉187160883

    P粉1871608832024-02-27 11:03:42

    The simple code below will extract what you need.

    After that, all you need to do is make an ajax/axios request to the controller.

    var url = 'http://cricketstats.test/en/dashboard/players/z2d4ca09a7/india/941/runs';
    var url_split = url.split("/");
    var my_id_location = url_split.length - 2;
    var my_id = url_split[my_id_location];
    console.log(my_id );

    Here are ideas on how to send data to a controller using ajax:

    $.ajax({
      url: "/your/url",
      method: 'POST',
      data: {id:my_id, _token:token},
      success: function(data) {
        //if success
      }
    });

    reply
    0
  • Cancelreply