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
@csrfis 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'sdestroyandupdateactions are likely configured to accept.
内容的提问来源于stack exchange,提问作者Idan Brinza

