Ad

How To Write Route URL Inside Javascript Laravel Blade?

- 1 answer

This is my route, having 2 parameters,

url: '{{ route('datatable.getaccess', [$room->id_project , $room->id]) }},

if write like that will shown : xxxxxx?xxxxxx , that have question markbeetwen id_project to $id , how to write correctly? because that should "/" slash

Thank you.

Ad

Answer

You can use string placeholders for javascript.

<input type="hidden" id="_room_id" value="{{ $room->id }}">
<input type="hidden" id="_room_project_id" value="{{ $room->id_project }}">
let project_id = $('#_room_project_id').val(); // or document.getElementById('_room_project_id').value if you're not using JQuery
let id = $('#_room_id').val();                 // or document.getElementById('_room_id').value if you're not using JQuery
let url = "{{ route('datatable.getaccess', [':project_id', ':id']) }}".replace(':project_id', project_id).replace(':id', id);

This looks wrong but it works since we're passing strings to the route helper (which in turn produces a string)

route('datatable.getaccess', [':project_id', ':id'])
// 'viewroom/:project_id/:id'

so

let url = "{{ route('datatable.getaccess', [':project_id', ':id']) }}".replace(':project_id', project_id).replace(':id', id);

is equivalent to

let url = "viewroom/:project_id/:id".replace(':project_id', project_id).replace(':id', id);
Ad
source: stackoverflow.com
Ad