I am working on SPA (Vue JS) and Laravel as API. I want when users will receive email to click on button that will sent them to specific route (already implemented).
So now i am struggling how to get the user id from the Vue JS part.
https://domain.test/admin/users/dashboard/user/65/show
65 from the URL is the user ID, so any idea how to extract that ID with java script from Vue JS?
I am not using vue router, this is Laravel route and Vue JS component is rendered from blade file.
I have implemented something like this bellow, but client refused...
https://domain.test/admin/users/dashboard/user/show?userId=65
let id = window.location.href.split('/').slice(-2)[0]
Since you're already rendering the page in a Blade file, you should already have access to the user ID server side.
In that case, simply add the variable server side. For example:
<a :href="'https://domain.test/admin/users/dashboard/user/' + {{
$user->id }} + '/show'">Go to Dashboard</a>
To get parmeters from url you can use vue router for that like this:
const routes = [
// dynamic segments start with a colon
{ path: '/users/:id', component: User },
]
const User = {
template: '<div>User {{ $route.params.id }}</div>',
}
Here is the link for more examples and vue router documentation:
https://router.vuejs.org/guide/essentials/dynamic-matching.html
You can also get parameters with only vanilla JavaScripe like follow:
const queryString = window.location.search;
console.log(queryString);
// ?product=shirt&color=blue&newuser&size=m
//then you have to parse the url
const urlParams = new URLSearchParams(queryString);
const color = urlParams.get('color')
console.log(color);
// blue