You need to show your sweet alert js code. The above doesn't really help.
How to make a delete confirmation Window with Sweet Alert
Let me be clear for you people :), I already found the answer to this issue in this link
https://laracasts.com/discuss/channels/laravel/confirm-delete-using-sweetalert
The problem is, I do not know how to implement that answer in my code, I just start using Sweet Alert today and I manage to setup basic messages like "Success! You are now loged in" kind of stuff.
Also I try to get an answer on Google, not having result's cause I don't know JavaScript either.
So, I'm asking for help here because I only need this kind of Window for my project (at least for now).
Laravel version: 5.4.19
S.O: Windows 10.
Enviroment: virtualized Homestead version 2.2.2 on a VirtualBox VM.
Additional packages: Laravel Collective for the forms and Sweet Alerts as I mentioned at the beginning of the thread.
This is my code:
ControladorProductos.php: (ProductsController in english)
<?php
public function destroy($id)
{
$productos = Producto::find($id);
if($productos != null) {
$productos->delete();
return redirect()->action('ControladorProductos@index');
}
return redirect()->action('ControladorProductos@index');
}
}
mostrar.blade.php (This is the view where I show the list of my products)
@extends('layouts.maestro')
@section ('contenido')
<hr>
@include('sweet::alert')
<table class="table table-responsive">
<th><a href="{{('/administrarStock/create')}}"><button type="button" class="btn-primary">Agregar un producto nuevo</button></a></th>
<th>
{!! Form::open(['method'=>'GET','url'=>'administrarStock','class'=>'navbar-form navbar-left','role'=>'search']) !!}
<div class="input-group custom-search-form">
<input type="text" name="search" class="form-control" placeholder="Search..">
<span class="input-group-btn">
<button type="submit" class="btn btn-primary">
</span>
</button>
</div>
{!! Form::close() !!}
</th>
</table>
<hr>
<table class="table">
<thead>
<tr>
<th>Codigo</th>
<th>Descripcion</th>
<th>Cantidad</th>
<th >Acciones</th>
<th>Precio Unitario</th>
</tr>
</thead>
<tbody>
<tr>
@foreach ($productos as $producto)
<td>{{$producto->codigo}}</td>
<td>{{$producto->descripcion}}</td>
<td>{{$producto->cantidad}}</td>
<td>
<a href="{{ route('administrarStock.edit',$producto->id) }}"> <button class="btn-primary"> Editar </button></a>
{!! Form::open(['method' => 'DELETE','route' => ['administrarStock.destroy', $producto->id],'style'=>'display:inline']) !!}
{!! Form::submit('Eliminar', ['class' => ' btn-danger']) !!}
{!! Form::close() !!}
</td>
<td> </td>
</tr>
@endforeach
</tbody>
</table>
{!! $productos->render() !!}
@endsection ('contenido')
I think that's all you guys need to help me, I hope that I'm being clear :D, thanks in advance.
@Screenbeetle Hi!
Yes, that's exactly how I did it (when I remove the Alert reference from the controller), the window is still not popping out, I'm starting thinking may be I installed Sweet-Alert in a wrong way and that's why I can do simple stuff but no use functions like the one in your code.
EDIT:
Don't kill me please for what I'm about to say since I did not know I have to add a reference to Jquery, so I add this references at the top of my "master layout":
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
And the Window is popping out, just not deleting the record.
EDIT 2:
I have managed to found a code that is working now, deleting the query after the confirmation. Thank you for all your help anyway :).
<script type="text/javascript">
document.querySelector('#confirm_delete').addEventListener('submit', function(e) {
var form = this;
e.preventDefault();
swal({
title: "¿Estas seguro que deseas eliminar este producto?",
text: "Si eliminas este producto, no podras recuperarlo",
type: "warning",
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: '¡Si, estoy seguro!',
cancelButtonText: "Cancelar",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal({
title: '¡Eliminado!',
text: 'El producto ha sido eliminado con exito!',
type: 'success'
}, function() {
form.submit();
});
} else {
swal("Cancelado", "Tu producto no ha sido eliminado", "error");
}
});
});
</script>
Please or to participate in this conversation.