首页 > 解决方案 > 需要将 laravel 中的表中的 id 传递给另一个刀片中的脚本

问题描述

这是single.blade.php

@foreach($device_property_List as $val)
<tr>
    <td>{{$val -> properties-> number_plate}}</td>                            

    <td>
        <input type="hidden" value="{{$val -> id}}"> <-this value need to pass in single_device.blade.php by ajax
        <input type="submit" id="{{$val -> id}}" class="btn btn-success btn-sm live" value="Live">
    </td>

    </tr>
@endforeach

这是此页面(single.blade.php)的脚本,用于将 id 发送到另一个页面(single_device.blade.php

这是single.blade.php

<script type="text/javascript">
$(document).on('click', '.live', function(){
    var id = $(this).attr('id');

    $.ajax({    
        window.location.href ="{{url('single_device')}} ->with (id)";              
    })
}); 
</script>

我需要在此脚本( single_device.blade.php)中获取此刀片中的 id 值

这是single_device.blade.php

<main class="main">
    <div id='map'></div>
</main>
<script>
var device_id = (here i need that id)
</script>

标签: javascriptphplaravel

解决方案


  1. 您可以使用 ajax 请求将 id 存储在会话中,然后在您想要的刀片中调用。
  2. 另一种方法是,在 url 中使用加密和解密的 id。

    use Illuminate\Support\Facades\Crypt;
    $key = Crypt::encrypt($key);
    
    <a href="{{route('single_device', ['id' => $key])}}">Some Text</a>
    $key = Crypt::decrypt($key); 
    

推荐阅读