最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to call laravel route from javascript function - Stack Overflow

programmeradmin0浏览0评论

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;

发布评论

评论列表(0)

  1. 暂无评论