Hello, I am trying to delete all the data from a table mysql
with a button from my project laravel
, I am doing it in the following way but it does not work for me, it comes out pagina expirada
, the code of my controller is the following.
public function borrarDatos(){
$marca=tbl_marca::all();
$marca->delete();
return back()->with('success', 'Datos Borrados Satisfactoriamente.');
}
In my view I have the following
<form action="{{ route('borrar') }}" class="form-horizontal" method="post" enctype="multipart/form-data">
<button class="btn btn-danger" type="submit">Eliminar Datos</button>
</form>
And my route is the following
Route::post('EliminarDatos', 'TestController@borrarDatos')->name('borrar');
How could I solve it? Thank you.
It
token
is necessary that this action is reliable, you solve that with:Right after the opening form tag
By the way the delete query can be reduced to
References
As you have been told, you need the @csrf token, your code would look like this:
edit:
Another way to do it and faster:
I would do a check with a
->first()
, to see if there is data in the table before doing the->delete()
:It is one of the ways to do it, like everything there are many more, for me the simplest. Hope this can help you