[Route:blalba.Destroy]的缺少必需参数

时间:2019-11-01 06:22:53

标签: laravel routes

错误

  

缺少[路由:brands.destroy] [URI:brands / {brand}]所需的参数。

我是第一次发现此错误,对此一无所知。

代码

  • 查看
@foreach ($brands as $key => $brand)
<tr>
    <td>{{ ++$i }}</td>
    <td>{{ $brand->name }}</td>
    <td>{{ $brand->detail }}</td>
    <td>
        <form action="{{ route('brands.destroy',$brand->id) }}" method="POST">
            <a class="btn btn-info" href="{{ route('brands.show',$brand->id) }}">Show</a>
            @can('product-edit')
                <a class="btn btn-primary" href="{{ route('brands.edit',$brand->id) }}">Edit</a>
            @endcan
            @csrf
            @method('DELETE')
            @can('product-delete')
                <button type="submit" class="btn btn-danger">Delete</button>
            @endcan
        </form>
    </td>
</tr>
@endforeach
  • 控制器
    public function destroy(Brand $brand)
        {
            $brand->delete();
            return redirect()->route('brands.index')
                ->with('success', 'Brand deleted successfully');
        }
  • 路线列表
+--------+-----------+---------------------------------+----------------------+------------------------------------------------------------------------+-------------------+
| Domain | Method    | URI                             | Name                 | Action                                                                 | Middleware        |
+--------+-----------+---------------------------------+----------------------+------------------------------------------------------------------------+-------------------+
|        | GET|HEAD  | brands                          | brands.index         | App\Http\Controllers\BrandController@index                             | web,auth          |
|        | POST      | brands                          | brands.store         | App\Http\Controllers\BrandController@store                             | web,auth          |
|        | GET|HEAD  | brands/create                   | brands.create        | App\Http\Controllers\BrandController@create                            | web,auth          |
|        | DELETE    | brands/{brand}                  | brands.destroy       | App\Http\Controllers\BrandController@destroy                           | web,auth          |
|        | PUT|PATCH | brands/{brand}                  | brands.update        | App\Http\Controllers\BrandController@update                            | web,auth          |
|        | GET|HEAD  | brands/{brand}                  | brands.show          | App\Http\Controllers\BrandController@show                              | web,auth          |
|        | GET|HEAD  | brands/{brand}/edit             | brands.edit          | App\Http\Controllers\BrandController@edit                              | web,auth          |
+--------+-----------+---------------------------------+----------------------+------------------------------------------------------------------------+-------------------+

4 个答案:

答案 0 :(得分:0)

为什么您以一种形式包含许多路线?

<form action="{{ route('brands.destroy',$brand->id) }}" method="POST">
            <a class="btn btn-info" href="{{ route('brands.show',$brand->id) }}">Show</a>
            @can('product-edit')
                <a class="btn btn-primary" href="{{ route('brands.edit',$brand->id) }}">Edit</a>
            @endcan
            @csrf
            @method('DELETE')
            @can('product-delete')
                <button type="submit" class="btn btn-danger">Delete</button>
            @endcan
        </form>

答案 1 :(得分:0)

这可能是因为您需要像这样定义参数。 <form action="{{ route('brands.destroy', ['brand' => $brand->id]) }}" method="POST">

您不必总是这样做,但是我遇到了我不得不这样做的问题。

如果不是这种情况,我会不知所措,因为这一切对我来说都很不错。

答案 2 :(得分:0)

您应该检查$brand->id是否为null

如果可能的话,将参数作为关联数组传递给route帮助器是明智的,至少是一个数组,但对于一个参数,它将采用单个值。如果您传递一个null值,就好像您根本没有传递参数一样。

答案 3 :(得分:0)

已修复

在以下位置更改视图中的一些代码:

<form action="{{ route('brands.destroy', $brand->id) }}" method="POST">
    ...
    ...
</form>

收件人:

<form action="{{ route('brands.destroy', [$brand->id_brand]) }}" method="POST">
    ...
    ...
</form>