I want to create a function that is useful for deleting one row of data in a table that I created. When I want to add data to the table, I get the error ‘The DELETE method is not supported for route pkl. Supported methods: GET, HEAD, POST.’
Route
Route::get('pkl', [FormController::class, 'pkl'])->middleware('isLogin');
Route::get('pkl2', [FormController::class, 'pkl2'])->middleware('isLogin');
Route::post('/pkl', [FormController::class, 'Up']); //post ke session
Route::post('/pkl2/form-kedua', [FormController::class, 'upFormKedua']);
Route::delete('/delete', 'FormController@deleteData')->name('data.delete');
Controller
public function deleteData(Request $request) {
$index = $request->input('index');
if (session('data_preview') && isset(session('data_preview')[$index])) {
session()->pull('data_preview.' . $index);
}
return redirect('/pkl');
}
View
<table class="table table-striped mt-5 text-center" id="tabelData">
<tr>
<th scope="col">No</th>
<th scope="col">Nama </th>
<th scope="col">NIS/NIM </th>
<th scope="col">Kontak </th>
<th scope="col">Jenis Kelamin </th>
<th scope="col">Jurusan </th>
<th scope="col"></th>
</tr>
@if (session('data_preview'))
@php $nomorUrut = 1; @endphp
@foreach (session('data_preview') as $data)
<tr>
<td>{{ $nomorUrut }}</td>
<td>{{ $data['participant_name'] }}</td>
<td>{{ $data['nisnim'] }}</td>
<td>{{ $data['participant_contact'] }}</td>
<td>{{ $data['gender'] == '1' ? 'Laki-Laki' : 'Perempuan' }}</td>
<td>{{ $data['department'] }}</td>
<td>
<form action="/delete" method="post">
@csrf
@method('delete')
<input type="hidden" name="index" value="{{ $loop->index }}">
<button type="submit" class="border border-0"><i class="bi bi-trash" style="color: red"></i></button>
</form>
</td>
</tr>
@php $nomorUrut++; @endphp
@endforeach
@endif
</table>
2
Answers
The problem is in your controller. You are redirecting to route
pkl
usingredirect()
in yourdeleteData
method. You can’t do that.Instead of that give your routes name using
name()
and then useredirect()->route()
like below,Controller
Route
Also u need to pass the id in route
Route::delete(‘/delete/{id}’, [FormController::class, ‘deleteData’])->name(‘data.delete’);