Laravel 6 - [路线:suppliers.destroy] [URI:供应商/{供应商}] 缺少必需的参数
Laravel 6 - Missing required parameters for [Route: suppliers.destroy] [URI: suppliers/{supplier}]
我有一个带按钮的索引页,用于从 table.
中删除一列
@foreach ($suppliers as $supplier)
<tr>
<th>{{ $supplier -> idSupplier }}</th>
<th style="color:blue;"><a href="/suppliers/{{$supplier->idSupplier}}">{{ $supplier -> column1 }}</a></th>
<th>{{ $supplier -> column2 }}</th>
<th>{{ $supplier -> column3 }}</th>
<th>{!! $supplier -> column4 !!}</th>
<th>
<a class="btn btn-warning" href="/suppliers/{{$supplier->idSupplier}}/edit" role="button">
<i class="fa fa-tools"></i>
Edit</a>
<a class="btn btn-danger" href="{{ action('SuppliersController@destroy') }}" role="button">
<i class="fa fa-eraser"></i>
Delete</a>
</th>
</tr>
@endforeach
但现在每次我打开我的索引页面时它都会给我这个错误信息
Facade\Ignition\Exceptions\ViewException Missing required parameters
for [Route: suppliers.destroy] [URI: suppliers/{supplier}]. (View:
C:\xampp\htdocs\Invent\resources\views\suppliers\index.blade.php)
这是我的路线
Route::resource('suppliers', 'SuppliersController');
这是来自 SuppliersController
的 destroy
函数
public function destroy($idSupplier)
{
$supplier = Supplier::find($idSupplier);
$supplier->delete();
return redirect('/suppliers')->with('success', 'Supplier removed');
}
我已经尝试了 ,它又给我一条错误消息。
好吧,您没有为控制器的操作传递所需的参数。 destroy
方法接收参数 idSupplier
以执行其操作。从 blade 中,您只是在不传递参数的情况下调用控制器操作。像下面这样:
<a class="btn btn-danger" href="{{ action('SuppliersController@destroy', ['idSupplier' => $supplier->idSupplier]) }}" role="button">
<i class="fa fa-eraser"></i>
Delete
</a>
但这行不通。您注册的路由方法是 DELETE
但它会重定向到 GET
方法。所以使用下面的方法进行删除。
<form action="{{ route('suppliers.destroy', $supplier->idSupplier) }}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger btn-sm" title="Delete">Delete</button>
</form>
我建议使用确认模式进行一些改进。然后我会向控制器提交表格。
以下示例考虑您使用 Bootstrap 和 laravelCollective。 (代码已简化)
1.删除按钮
<a href="#" data-toggle="modal" data-target="#modal1">Delete</a>
2。模态
<div class="modal" id="modal1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-footer">
<button type="button" data-dismiss="modal">Cancel</button>
<button type="button">Delete</button>
</div>
</div>
</div>
</div>
3。表格
{{ Form::open(['method' => 'DELETE', 'route' => array('suppliers.destroy', $supplier->id) ]) }}
{{ Form::hidden('id', $supplier->id) }}
<button type="submit">Delete</button>
{{ Form::close() }}
注意供应商 ID ($idSupplier) 是如何从表单传递到控制器的。
您正在调用不带参数的 de destroy 方法,您可以这样做
@foreach ($suppliers as $supplier)
<tr>
<th>{{ $supplier -> idSupplier }}</th>
<th style="color:blue;"><a href="/suppliers/{{$supplier->idSupplier}}">{{ $supplier -> column1 }}</a></th>
<th>{{ $supplier -> column2 }}</th>
<th>{{ $supplier -> column3 }}</th>
<th>{!! $supplier -> column4 !!}</th>
<th>
<a class="btn btn-warning" href="/suppliers/{{$supplier->idSupplier}}/edit" role="button">
<i class="fa fa-tools"></i>
Edit</a>
<a class="btn btn-danger" href="{{ route('suppliers.destroy',$supplier->idSupplier ) }}" role="button">
<i class="fa fa-eraser"></i>
Delete</a>
</th>
</tr>
@endforeach
我有一个带按钮的索引页,用于从 table.
中删除一列@foreach ($suppliers as $supplier)
<tr>
<th>{{ $supplier -> idSupplier }}</th>
<th style="color:blue;"><a href="/suppliers/{{$supplier->idSupplier}}">{{ $supplier -> column1 }}</a></th>
<th>{{ $supplier -> column2 }}</th>
<th>{{ $supplier -> column3 }}</th>
<th>{!! $supplier -> column4 !!}</th>
<th>
<a class="btn btn-warning" href="/suppliers/{{$supplier->idSupplier}}/edit" role="button">
<i class="fa fa-tools"></i>
Edit</a>
<a class="btn btn-danger" href="{{ action('SuppliersController@destroy') }}" role="button">
<i class="fa fa-eraser"></i>
Delete</a>
</th>
</tr>
@endforeach
但现在每次我打开我的索引页面时它都会给我这个错误信息
Facade\Ignition\Exceptions\ViewException Missing required parameters for [Route: suppliers.destroy] [URI: suppliers/{supplier}]. (View: C:\xampp\htdocs\Invent\resources\views\suppliers\index.blade.php)
这是我的路线
Route::resource('suppliers', 'SuppliersController');
这是来自 SuppliersController
destroy
函数
public function destroy($idSupplier)
{
$supplier = Supplier::find($idSupplier);
$supplier->delete();
return redirect('/suppliers')->with('success', 'Supplier removed');
}
我已经尝试了
好吧,您没有为控制器的操作传递所需的参数。 destroy
方法接收参数 idSupplier
以执行其操作。从 blade 中,您只是在不传递参数的情况下调用控制器操作。像下面这样:
<a class="btn btn-danger" href="{{ action('SuppliersController@destroy', ['idSupplier' => $supplier->idSupplier]) }}" role="button">
<i class="fa fa-eraser"></i>
Delete
</a>
但这行不通。您注册的路由方法是 DELETE
但它会重定向到 GET
方法。所以使用下面的方法进行删除。
<form action="{{ route('suppliers.destroy', $supplier->idSupplier) }}" method="POST">
@csrf
@method('DELETE')
<button class="btn btn-danger btn-sm" title="Delete">Delete</button>
</form>
我建议使用确认模式进行一些改进。然后我会向控制器提交表格。
以下示例考虑您使用 Bootstrap 和 laravelCollective。 (代码已简化)
1.删除按钮
<a href="#" data-toggle="modal" data-target="#modal1">Delete</a>
2。模态
<div class="modal" id="modal1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-footer">
<button type="button" data-dismiss="modal">Cancel</button>
<button type="button">Delete</button>
</div>
</div>
</div>
</div>
3。表格
{{ Form::open(['method' => 'DELETE', 'route' => array('suppliers.destroy', $supplier->id) ]) }}
{{ Form::hidden('id', $supplier->id) }}
<button type="submit">Delete</button>
{{ Form::close() }}
注意供应商 ID ($idSupplier) 是如何从表单传递到控制器的。
您正在调用不带参数的 de destroy 方法,您可以这样做
@foreach ($suppliers as $supplier)
<tr>
<th>{{ $supplier -> idSupplier }}</th>
<th style="color:blue;"><a href="/suppliers/{{$supplier->idSupplier}}">{{ $supplier -> column1 }}</a></th>
<th>{{ $supplier -> column2 }}</th>
<th>{{ $supplier -> column3 }}</th>
<th>{!! $supplier -> column4 !!}</th>
<th>
<a class="btn btn-warning" href="/suppliers/{{$supplier->idSupplier}}/edit" role="button">
<i class="fa fa-tools"></i>
Edit</a>
<a class="btn btn-danger" href="{{ route('suppliers.destroy',$supplier->idSupplier ) }}" role="button">
<i class="fa fa-eraser"></i>
Delete</a>
</th>
</tr>
@endforeach