Answer the question
In order to leave comments, you need to log in
Is the deletion of records implemented correctly?
I'm learning Laravel, just started
. I'm making a simple admin panel for a one-pager.
I did not find an example of how to delete records.
I did it as I thought :)
Most likely there is a more elegant solution to this issue.
Can you point out the mistakes? Thanks Controller
Part
class SlideController extends Controller
{
public function destroy($id){
Slide::destroy($id);
return redirect()->back();
}
}
<!-- Делаем удаление записей -->
<div id="url" data-url="{{ $urlDel = Route::getCurrentRoute()->getPath() }}">
{{ Form::open(array('route' => array('admin.slides.destroy', 0), 'method' => 'delete')) }}
{{ Form::close() }}
</div>
<script>
$('span.del').click(function(){
var getId = $(this).data('id');
var actionUrl = '/' + $('#url').data('url') + '/'+getId;
var data = $('#url form').serialize();
$.ajax({
type: 'POST',
url: actionUrl,
data: data,
success: function(data) {
// 'tr#row'+getId - строка с ID порписан айдишник в шаблоне
$('tr#row'+getId).fadeOut('slow');
},
error: function (data) {
alert('Произошла досадная ошибка. Запись не была удалена. Попробуйте еще раз пожалуйста'+data);
}
});
//$('#url form').attr('action', actionUrl).submit();
})
</script>
<!-- Делаем удаление записей -->
Answer the question
In order to leave comments, you need to log in
- Why form? There it is absolutely not needed
- Why return redirect()->back(); if the request goes through ajax?
- It is customary to put a confirmation on deletion (the same alert('Are you sure you want to destroy the world?'))
- It is better to use not POST, but DELETE (or emulate POST + hidden field _method="DELETE")
-
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question