My route:
Route::get('deleterequest/{request_id}', 'RequestController@getDeleteRequest')->name('getDeleteRequest');
In view, i want to click a tag to call this route View:
<a onclick=" confirmDelete({{ $request->task_id }})" href="#" ><span class="fa fa-trash-o"></span></a>
<script type="text/javascript">
function confirmDelete(id){
document.location.href="{!! route('getDeleteRequest', $id); !!}";
}
when i run the code, it show error message:
Undefined variable: id
My route:
Route::get('deleterequest/{request_id}', 'RequestController@getDeleteRequest')->name('getDeleteRequest');
In view, i want to click a tag to call this route View:
<a onclick=" confirmDelete({{ $request->task_id }})" href="#" ><span class="fa fa-trash-o"></span></a>
<script type="text/javascript">
function confirmDelete(id){
document.location.href="{!! route('getDeleteRequest', $id); !!}";
}
when i run the code, it show error message:
Share Improve this question asked Mar 28, 2018 at 2:49 Trọng KoyTrọng Koy 211 gold badge1 silver badge2 bronze badgesUndefined variable: id
2 Answers
Reset to default 3I just you need to parse it and replace..
function confirmDelete(id){
let url = "{{ route('getDeleteRequest', ':id') }}";
url = url.replace(':id', id);
document.location.href=url;
}
or just
<a onclick="confirmDelete({{route('getDeleteRequest', $request->task_id) }})" href="#" ><span class="fa fa-trash-o"></span></a>
function confirmDelete(url){
document.location.href=url;
}
goto your_project_folder\config and create file constants.php if it's not exist and then define site_path constant inside it
define('SITE_PATH', 'http://www.yoursite/');
- and then use like this
window.location = SITE_PATH+'deleterequest/'+id;