I'm trying to add a form button to delete a record, but for some reason the csrf token doesn't insert it. I've tried many ways but I can't get it to work. Any suggestions?
<div class="row">
<a href="{{ route('publicaciones.show', $id)}}" class="btn btn-success btn-sm h-25 d-inline-block mr-3">Ver</a>
<a href="{{ route('publicaciones.show', $id)}}" class="btn btn-primary btn-sm h-25 d-inline-block">Editar</a>
<form method="POST" action="{{ url("publicaciones/{$id}") }}" class="col">
@csrf
@method('DELETE')
<button class="btn btn-danger btn-sm" type="submit" id="eliminar">Eliminar</button>
</form>
</div>
Attached image :
Usually your route doesn't use web middleware. Make sure you use routes/web.php
for the route and also make sure RouteServiceProvider
has Route::middleware('web')
example:
/**
* Define the "web" routes for the application.
*
* These routes all receive session state, CSRF protection, etc.
*
* @return void
*/
protected function mapWebRoutes()
{
Route::middleware('web')->namespace($this->namespace)->group(base_path('routes/web.php'));
}
or you can add manually to each route use like bellow:
Route::group(['middleware' => ['web']], function () {
// your routes here
});
As commented on RouteServiceProvider
web middleware will make your route receive session state, CSRF protection, etc.