Alright so, for my school project I have to delete a container out of my database. I've made forms before that would delete rows but this time i'm running into a error that I cannot resolve. I've gone through many posts on multiple forums but at this point I'm raging non stop cuz there seems to be no fix
Error: Property [ID] does not exist on this collection instance.
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',App\Http\Controllers\containerController::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');
}
The model is Container and has the following tables:
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 >Alle Containers</h3>
<a href="/run" style="margin: 0 auto; display:block;">Voeg Extra Container toe</a>
<table >
<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
Tried multiple solutions from stack overflow and other forums, I'm working with the most updated Laravel so I don't know what the issue could be
CodePudding user response:
in your web.php not need define this line:
Route::get("container/{id}",[App\Http\Controllers\containerController::class, 'destroy']);
for solve problem need to pass container to your blade, for this you should write this code in your containerController
action edit
/show
(which one use in your project):
public function edit($id)
{
$container = YOUR_MODEL_NAME::find($id);
return view('YOUR_VIEW_NAME', compact('container'));
}
in your blade change form like bellow:
@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
and i think everything work very well.
also for getting more information, can read laravel document
and for view defined route run this command
php artisan route:list
CodePudding user response:
You are passing $container in index function, not $containers...
change like below :
public function index(Request $request)
{
//$container = Container::all();
$containers = Container::all();
return view('container.index')
->with('containers',$containers);
}