Dazzle
Dazzle

Reputation: 59

How to Write Route URL inside Javascript Laravel Blade?

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 mark beetwen id_project to $id , how to write correctly? because that should "/" slash

Thank you.

Upvotes: 2

Views: 3798

Answers (1)

IGP
IGP

Reputation: 15786

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);

Upvotes: 3

Related Questions