I am creating a CRUD of products. In the index I have the grid with the registers. In each record there are two buttons (edit and delete). Editing works fine for me, but not editing. I need to press that button to show me a view (delete.blade.php) that is a form where the data of that record should appear, and when I click on the delete button on that form, I will delete it. I have tried in many ways, but I put the code of what I have now. In ProductController.php:
public function borrar($id){
$producto=\App\Producto::find($id);
return view('productos.borrar')->with('producto',$producto);
}
public function destroy($id){
$producto=\App\Producto::find($id);
$producto->destroy($id);
return view('productos.destroy',['id'=>$id]);
}
In delete.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Eliminar Producto</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class='container'>
<div class='jumbotron'>
<h1>Eliminar Producto</h1>
<p>Introduce todos los datos para eliminar un producto en la base de datos.</p>
</div>
<form name='formularioeliminar' action='{{'productos/destroy'}}' method='get'>
<div class='form-group'>
{{csrf_field()}}
<label for='id'>Introduce el id del producto:</label>
<input type='number' class='form-control' placeholder='Introduce el id' name='id' value="{{$producto->id}}"><br>
<label for='id'>Introduce el nombre del producto:</label>
<input type='text' class='form-control' placeholder='Introduce el nombre' name='nombre' value="{{$producto->nombre}}" size=30><br>
<label for='precio'>Introduce el precio del producto:</label>
<input type='decimal' class='form-control' placeholder='Introduce el precio' name='precio' value="{{$producto->precio}}"><br>
</div>
<div class='botons-group'>
<input type='submit' name='eliminar' value='ELIMINAR'>
<input type="button" onclick="history.back()" name="volver" value="VOLVER">
</div>
</form>
</div>
<?php
if(isset($_GET['borrar'])){
//buscar();
?>{{productos.borrar}}<?php
}
if(isset($_GET['volver'])){
//header("Location:productos/index");
?> <a href="{{ url('productos.index') }}"></a> <?php
}
?>
</body>
</html>
In destroy.blade.php:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Eliminar Producto</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<?php
function conectar(){
$con=mysqli_connect("localhost","root","","dam2d-laravel");
return $con;
}
function desconectar(){
mysqli_close(conectar());
}
conectar();
$id=isset($_GET['id']) ? $_GET['id'] : '';
$nombre=isset($_GET['nombre']) ? $_GET['nombre'] : '';
$precio=isset($_GET['precio']) ? $_GET['precio'] : '';
function eliminar(){
$id=$_GET['id'];
$nombre=$_GET['nombre'];
$precio=$_GET['precio'];
$esta=false;
if(conectar()->connect_error){
die('Error de conexion: '.conectar()->connect_error);
}else{
$sql="SELECT * FROM productos where id='".$id."'";
$resul=mysqli_query(conectar(),$sql);
while($linea=mysqli_fetch_array($resul)){
$esta=true;
}
if($esta==false){
echo "No existe ningún producto con el id ".$id;
}
if(!mysqli_query(conectar(),"DELETE FROM productos WHERE id='".$id."'")){
echo "No se ha eliminado el producto!".mysqli_error(conectar());
}else{
echo "Producto eliminado!";
}
}
}
desconectar();
?>
</body>
</html>
In web.php:
Route::resource('productos','ProductoController');
Route::view('productos/borrar/{id}','ProductoController@borrar');
The button on the index grid:
<a href="{{route('productos.borrar',$producto->id)}}" class="btn btn-danger">ELIMINAR</a>
You do not even show me the delete page. I think the problem could be in the way of calling some view or some variable that you have to go to the form delete.blade.php form.