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

Laravel视图解析错误排查:用户列表跳转详情页报错

Laravel用户列表跳转链接语法错误修复及路由调整

错误信息

出现解析错误:syntax error, unexpected token ";", expecting ")",触发场景为在用户列表表格中添加详情页跳转链接时。

相关代码

用户列表视图(Blade)

<table class="user-list-table table">
    <thead class="table-light">
        <tr>
            <th></th>
            <th>Name</th>
            <th>Email</th>
            <th>Password</th>
            <th>Phone Number</th>
            <th>Admin</th>
        </tr>
    </thead>
    <tbody>
        @foreach($users as $item)
        <tr>
            <td>{{ $loop->iteration }}</td>
            <td>
                <a 
                href="{{ route('user-view', ['id' => $item->id] }}">
                {{ $item->name }}
            </a>
            </td>
            <td>{{ $item->email }}</td>
            <td>{{ $item->password }}</td>
            <td>{{ $item->phone_number }}</td>
            <td>{{ $item->is_admin }}</td>

        </tr>
        @endforeach
    </tbody>
</table>

UserController

public function show($id)
{
    $user = User::find($id);
    return view('main.user.user-view-account')->with('user', $user);
}

当前路由配置

Route::get('user-view/{id}', [UserController::class, 'show'])->name('user-view');

错误原因

视图中调用route()函数时存在语法错误:route('user-view', ['id' => $item->id]缺少闭合的圆括号,导致Blade模板解析PHP代码时无法识别正确的函数结构,触发语法错误。

修复步骤

  1. 修正视图中的跳转链接
    将<a>标签的href属性修改为:
href="{{ route('user-view', ['id' => $item->id]) }}"

补上route()函数最后的闭合圆括号,确保PHP语法正确。

  1. 调整路由以匹配期望路径
    若期望路由路径为user/{id}/view,修改路由配置为:
Route::get('user/{id}/view', [UserController::class, 'show'])->name('user-view');

此配置会生成符合user/1/view格式的URL,控制器的show方法无需修改,仍可正常接收$id参数。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 06:16:33