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

Laravel视图报错:destroy/update方法调用语法错误致页面无法显示

Hey there, let's get this sorted out quickly!

The syntax errors you're seeing are caused by an incorrect use of the @ symbol in your Blade template code. Here's what's wrong and how to fix it:

The Core Issue

You added an unnecessary @ before task->id inside the action() helper function. In Blade, @ is used for things like echoing variables safely (e.g., {{ @$task->id }} to avoid errors if $task is null) or for control directives, but when you're passing a variable as an argument to a PHP function like action(), you should use the standard PHP variable syntax $task->id instead.

Fixed Code for index.blade.php (Destroy Form)

Your original line:

<form method = 'POST' action ="{{action('TaskController@destroy', @task->id)}}">

Corrected version (plus critical Laravel requirements):

<form method="POST" action="{{ action('TaskController@destroy', $task->id) }}">
    @csrf
    @method('DELETE')
    <!-- Add your submit button here, e.g.: -->
    <button type="submit">Delete Task</button>
</form>

Fixed Code for edit.blade.php (Update Form)

Your original line:

<form method = 'POST' action = "{{action('TaskController@update' , @task->id)}}">

Corrected version:

<form method="POST" action="{{ action('TaskController@update', $task->id) }}">
    @csrf
    @method('PUT')
    <!-- Add your form fields for updating the task here -->
    <button type="submit">Update Task</button>
</form>

Extra Notes

  • @csrf is mandatory: Laravel requires this token on all POST forms to prevent cross-site request forgery attacks. Omitting it will cause a validation error.
  • @method('DELETE')/@method('PUT'): HTML forms only support GET and POST methods, so Laravel uses these directives to simulate the DELETE/PUT methods that your controller's destroy and update actions are likely configured to accept.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 09:27:15