0

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 : Capture

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
Alberto
  • 163
  • 2
  • 13

1 Answers1

0

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.

Muhammad Dyas Yaskur
  • 6,914
  • 10
  • 48
  • 73
  • I think the problem is not this, because the CSRF in any other view adds it and they have the same kind of routes. I think the problem comes because this datatable uses ajax and for some reason I don't know, it doesn't insert the CSRF. – Alberto Oct 23 '20 at 10:34
  • how you call the ajax route and where is it? – Muhammad Dyas Yaskur Oct 23 '20 at 10:39
  • Excuse me. You're right. The datatable was loaded by an API route. I've already solved it thanks to your information. Thanks a lot! – Alberto Oct 23 '20 at 10:46