To concatenate a JavaScript variable to a route in a Blade template file in Laravel, you can use the following syntax:
<a href="{{ route('route-name', ['parameter' => ' + jsVariable + ']) }}">Link</a>
This will generate an anchor tag with the href attribute set to the route with the JavaScript variable appended as a route parameter.
Here’s an example of how you might use this in a Blade template:
<script>
let jsVariable = 'foo';
</script>
<a href="{{ route('route-name', ['parameter' => ' + jsVariable + ']) }}">Link</a>
This will generate an anchor tag with the href attribute set to /route-name/foo
.
I hope this helps! Let me know if you have any questions.
Method 2:
Another way would be replacing a substring using the replace method in Javascript.
Here’s an example:
let status = "new";
let duration = "today";
var url = "{{route('admin.support.getTicketsByStatusDuration',['status' => ':status', 'duration' => ':duration']) }}";
url = url.replace(':status', status).replace(':duration',duration);
// Output local http://localhost:8000/admin/tickets/new/today