将Extract an ID from a URL and transmit it to the Laravel controller in VueJS
P粉754477325
P粉754477325 2024-02-26 13:06:55
0
1
402

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粉754477325
P粉754477325

reply all(1)
P粉187160883

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
  }
});
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template