LaraBABA's avatar

Do we really need a form to delete data?

Hello,

I am following multiple tutorials and each time people use a form to delete a row of data via a button. Is there another cleaner way of doing this please? I find this very strange. I understand it is for security purpose but I am a bit surprised that there isn't a clever thing in Laravel that makes the deletion quicker to format.

Thank you.

0 likes
9 replies
LaraBABA's avatar

Ok this seems to be the cleanest methods I found

{!! Form::open(['method' => 'DELETE','route' => ['articles.destroy', $article->id],'style'=>'display:inline']) !!}

{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}

{!! Form::close() !!}
36864's avatar

Your definition of the term "clean" is rather strange.

Forms are used to set the http method to something other than GET. This prevents users from deleting resources by just typing something into their address bar.

If you really wanted to, you could have a GET route for deletions, but that's a terrible idea and I don't feel comfortable giving an example that people might actually use.

tisuchi's avatar

@Boubou

I am bit strange after seeing your cleaner code.

You know what, I think the meaning of "Cleanest" id different to you. The real truth is that, the level of cleanest is very subjectives.

Might be this code is fine to you, however, if you ask me, well, I never ever practice this way. I don't like it.

2 likes
LaraBABA's avatar

What I meant by that is something like action="delete/{healper or something}/{id}" which would have created the deletion of the user.

Yes I totally understand the POST and GET methods, but it was my understanding that frameworks would have "somehow"(if I knew how I would have created it myself..) created some kind of helpers that replaces the whole form(code) by just a couple of words.

Hope you understand what I mean by "clean"

LaraBABA's avatar

Here is an example from Yii2:

echo DetailView::widget([
    'model'=>$model,
    'attributes'=>$attributes,
    'deleteOptions'=>[
    'url'=>['delete', 'id' => $model->id], <--------------This part
    'data'=>[
    'confirm'=>Yii::t('app', 'Are you sure you want to delete this record?'),
    'method'=>'post',
    ], ] ]);

Rather than this at the moment:

<a href="#" onclick="var result = confirm(""); if(result){even.preventDefault(); document.getElementById('logout-form').submit();}">Delete</a>
<form id="logout-form" action="{{ route('users.detroy', [$company->id]) }}" method="POST" style="display: none;">
<input type="hidden" name="_method" value="delete">
{{ csrf_field() }}
</form>
ejdelmonico's avatar

Just use a button and send a delete request with axios. Otherwise, you need to use a form so that you can post a delete request using a hidden field in Laravel. {{ method_field('DELETE') }}

1 like
LaraBABA's avatar

Thanks, Never heard about Axios, will read about it now...

Snapey's avatar
Snapey
Best Answer
Level 122

clean to me is more like

<form action="{{ route('thing.delete', $item) }}" method="POST">
{{ csrf_field() }}
<input type="hidden" name="_method" value="DELETE" />
<button type="submit"><i class="fa fa-trash" /></i></button>
</form>

as only the route and the model need to change then it would be quite easy to create a blade directive like

   @deleteModel(thing.delete, $item)

The issue here is that implementation in this case uses bootstrap button and font-awesome icons so it's very specific to this implementation. This is probably why it's not boilerplated in the framework

2 likes
LaraBABA's avatar

Thanks Snapey, good example and good point:-)

Please or to participate in this conversation.