Volka Dimitrev
Volka Dimitrev

Reputation: 387

Fetch an ID from a URL and pass it to the laravel controller in VueJS

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

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

Here 941 is the player id. I want this id also to be passed to the laravel controller along with the remaining form data.

Is there have anyway to fetch this id from the url in vue, and pass it to the laravel controller?

Upvotes: 0

Views: 620

Answers (1)

kapitan
kapitan

Reputation: 2212

The simple code below will extract what you need.

After this, all you need to do is make an ajax/axios request going 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's an idea about how you can send the data to the controller using ajax:

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

Upvotes: 1

Related Questions