how to call laravel route from javascript function - Stack Overflow

My route:Route::get('deleterequest{request_id}', 'RequestController@getDeleteRequest�

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:

Undefined variable: id

Share Improve this question asked Mar 28, 2018 at 2:49 Trọng KoyTrọng Koy 211 gold badge1 silver badge2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742408994a4438430.html

相关推荐

  • how to call laravel route from javascript function - Stack Overflow

    My route:Route::get('deleterequest{request_id}', 'RequestController@getDeleteRequest�

    13小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信