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

表格中Delete按钮无法正常工作,请求技术协助排查解决

Fixing Your Laravel Delete Button Issue

Let's walk through why your Delete button isn't working and get it sorted out quickly!

The Core Problem

It looks like you're wrapping your table cell HTML in a single-quoted string, which is preventing Laravel's Blade template engine from parsing variables like {{ $patient->id }} and helper functions properly. When these don't get compiled into actual values, your form submits to the wrong URL or fails CSRF validation, so the delete action never fires.

Step-by-Step Fixes

1. Use Blade Template Syntax Directly (Best Practice)

Instead of wrapping the HTML in single quotes, write it directly in your Blade view file. Blade will automatically parse the variables and directives:

<td class="text-center ">
    <form action="/patients/{{ $patient->id }}" method="post" class="delete_link">
        @csrf
        @method('DELETE')
        <button type="submit" class="btn btn-danger btn-sm pull-right">Delete</button>
    </form>
</td>
  • @csrf replaces csrf_field() and generates the required CSRF token hidden input.
  • @method('DELETE') replaces method_field('DELETE') to simulate the DELETE HTTP method (since HTML forms only support GET/POST).
  • {{ $patient->id }} will now correctly render the actual patient ID in the form action URL.

2. If You Must Use PHP String Output

If you're generating this HTML inside a PHP block (using echo), avoid single quotes and use PHP variable interpolation instead:

echo '<td class="text-center ">
    <form action="/patients/' . $patient->id . '" method="post" class="delete_link">
        ' . csrf_field() . '
        ' . method_field('DELETE') . '
        <button type="submit" class="btn btn-danger btn-sm pull-right">Delete</button>
    </form>
</td>';

Here we're concatenating the PHP variable $patient->id and the output of csrf_field()/method_field() directly into the string, since Blade syntax won't work inside PHP single-quoted strings.

3. Verify Key Elements Work

After making the changes, check your page source to confirm:

  • The form's action attribute shows the correct patient ID (e.g., /patients/123).
  • There's a hidden input for the CSRF token: <input type="hidden" name="_token" value="...">.
  • There's a hidden input for the DELETE method: <input type="hidden" name="_method" value="DELETE">.

4. Confirm Your Route Exists

Make sure you have a DELETE route defined in routes/web.php:

Route::delete('/patients/{patient}', [PatientController::class, 'destroy']);

This tells Laravel to route DELETE requests to the destroy method in your PatientController.

内容的提问来源于stack exchange,提问作者R. Mseti

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:17:01