You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel路由报错:缺失cicilan路由所需id参数求助

问题排查与解决方案

错误根源

问题出在Datatables按钮渲染的路由生成逻辑里:Blade模板会优先解析{{ route("cicilan", ":id") }},此时你传入的是字符串:id,Laravel的route()函数无法识别这个占位符,会直接抛出缺少路由参数的异常,根本轮不到JS执行替换步骤。

修复方案

以下两种方法任选其一:

方法一:用基础URL拼接ID

直接通过url()获取/cicilan的基础路径,再用JS拼接具体ID:

data: 'statusPelunasan',
name: 'statusPelunasan',
render: function (data, type, row) {
    if (row.statusPelunasan === 0 && row.status === 0) {
        return '<strong style="color: #BFBF00;">Belum Diterima</strong>';
    } else if (row.statusPelunasan === 0 && row.status === 1) {
        return '<a class="btn btn-info cicilanBtn" href="{{ url("/cicilan") }}/' + row.idPengajuan + '">Cicilan</a>';
    } else if (row.statusPelunasan === 1 && row.status === 1) {
        return '<strong style="color: green;">Lunas</strong>';
    } else {
        return '';
    }
}

方法二:正确使用路由占位符替换

先让Blade生成带临时占位符的路由,再用JS替换成实际ID:

data: 'statusPelunasan',
name: 'statusPelunasan',
render: function (data, type, row) {
    if (row.statusPelunasan === 0 && row.status === 0) {
        return '<strong style="color: #BFBF00;">Belum Diterima</strong>';
    } else if (row.statusPelunasan === 0 && row.status === 1) {
        let baseRoute = "{{ route('cicilan', ['id' => 'TEMP_ID']) }}";
        let actualRoute = baseRoute.replace('TEMP_ID', row.idPengajuan);
        return '<a class="btn btn-info cicilanBtn" href="' + actualRoute + '">Cicilan</a>';
    } else if (row.statusPelunasan === 1 && row.status === 1) {
        return '<strong style="color: green;">Lunas</strong>';
    } else {
        return '';
    }
}

验证步骤

  1. 修复后查看页面源码,确认生成的按钮href是http://localhost:8000/cicilan/44这类正确格式
  2. 访问该URL,检查控制器是否能正常接收$id参数,数据库查询是否返回对应数据

内容的提问来源于stack exchange,提问作者Bubleygum

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 10:05:11