属性[ID]在此集合实例上不存在.表单错误



好了,所以,对于我的学校项目,我必须从数据库中删除一个容器。我做了表单之前,会删除行,但这次我遇到了一个错误,我无法解决。我已经浏览了很多论坛上的帖子,但在这一点上,我一直在愤怒,因为似乎没有修复

错误:属性[ID]不存在。

index.blade.php→

<form action="{{ route('container.destroy',$container->ID) }}" action="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" title="Delete Post">Delete</button>
</form>

web.php→

Route::resource('container',AppHttpControllerscontainerController::class);

containerController.php→

public function index(Request $request)
{
$container = Container::all();
return view('container.index')->with('container',$container);
}
public function destroy($id)
{
$container = Container::find($id);
$container->delete();
return redirect('container')->with('status', 'Container is Opgehaald');
}

模型为Container,有以下表:

ID
ContainerPortal
PlaatsAvailable
Herkomst
Bestemming
VervoerderAankomst
VervoerderVertrek
OphaalDatum
Move0
Move1
Move2
Move3
Move4
Move5
updated_at
created_at

index.blade.php:

@extends('layouts.app')
@section('content')
<h3 class="text-center p-4 fontOne">Alle Containers</h3>
<a href="/run" class="btn btn-outline-success btn-block p-3 fontOne" style="margin: 0 auto; display:block;">Voeg Extra Container toe</a>
<table class="table table-bordered">
<thead>
<tr>
<th scope="col">Container</th>
<th scope="col">Portal</th>
<th scope="col">Herkomst</th>
<th scope="col">Bestemming</th>
<th scope="col">Ophaal Datum</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
@if(!$containers)
<p>Kon geen containers vinden</p>
@else
@foreach($containers as $container)
<tr>
<th scope="col">Container: {{$container->ID}}</th>
<td>Container Portal: {{$container->ContainerPortal}}</td>
<td>Herkomst: {{$container->Herkomst}}</td>
<td>Bestemming: {{$container->Bestemming}}</td>
<td>Ophaal Datum: {{$container->OphaalDatum}}</td>
<td>
<form action="{{ route('container.destroy',$container->ID) }}" action="POST">
{{ method_field('DELETE') }}
{{ csrf_field() }}
<button type="submit" title="Delete Post">Delete</button>
</form>
</td>
</tr>
@endforeach
@endif
</tbody>
</table>
@endsection

从堆栈溢出和其他论坛尝试了多种解决方案,我正在使用最新的Laravel,所以我不知道问题可能是什么

Update动动脑子后,我发现表单上有个错别字,是action="POST"而不是method="POST">

在你的web.php中不需要定义这一行:

Route::get("container/{id}",[AppHttpControllerscontainerController::class, 'destroy']);

解决问题需要将容器传递到刀片,为此,您应该在containerController操作edit/show(在您的项目中使用)中编写此代码:

public function edit($id)
{
$container = YOUR_MODEL_NAME::find($id);

return view('YOUR_VIEW_NAME', compact('container'));
}

在你的刀片改变形式像下面:

@foreach($container as $container_item)
<tr>
<th scope="col">Container: {{$container_item->ID}}</th>
<td>Container Portal: {{$container_item->ContainerPortal}}</td>
<td>Herkomst: {{$container_item->Herkomst}}</td>
<td>Bestemming: {{$container_item->Bestemming}}</td>
<td>Ophaal Datum: {{$container_item->OphaalDatum}}</td>
<td>
<form action="{{ route('container.destroy',$container_item->ID) }}" action="POST">
@method('DELETE')
@csrf
<button type="submit" title="Delete Post">Delete</button>
</form>
</td>
</tr>
@endforeach

我觉得一切都很好。

也可获取更多信息,可阅读laravel文档

资源控制器

对于view定义的路由,运行这个命令

php artisan route:list

您在索引函数中传递的是$container,而不是$containers…

更改如下:

public function index(Request $request)
{
$containers = Container::all();
return view('container.index')
->with('containers',$containers);
}

最新更新