As I did not manage to use datatables, I managed to improvise a different method. One that opens a form that edits the tables, the one to erase since it is done, can ignore that.
For some reason, the view does not appear. It says 404 error.
The view in home.blade
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Nombre</th>
<th>Precio</th>
<th>Fecha de inscripción</th>
<th>Acciones</th>
</tr>
</thead>
<tbody>
@foreach($product as $products)
<tr>
<td>{{$products['id']}}</td>
<td>{{$products['producto']}}</td>
<td>{{$products['precio']}}</td>
<td>{{$products['empresa']}}</td>
<td>
<form action="{{route('home.destroy', $products->id)}}" method="POST">
@csrf
{{ method_field('DELETE') }}
<input name="_method" type="hidden" value="DELETE">
<button class="btn btn-danger" type="submit">Borrar</button>
</form>
</td>
<td><a href="{{action('HomeController@edit', $products['id'])}" class="btn btn-warning">Editar</a></td>
</tr>
@endforeach
</tbody>
</table>
The Controller.
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$product=\App\Product::all();
return view('home',compact('product'));
}
public function sendData(Request $request){
$this->validate($request, [
'nombre' => 'required',
'precio' => 'required',
'empresa' => 'required'
]);
$product = new product([
'nombre' => $request->get('nombre'),
'precio' => $request->get('precio'),
'empresa' => $request->get('empresa')
]);
$product->save();
return redirect()->route('home')->with('success', 'Data Added');
}
public function edit($id)
{
$products = Product::find($id);
return view('edit',compact('products','id'));
}
public function update(Request $request, $id)
{
$this->validate($request, [
'producto' => 'required',
'precio' => 'required',
'empresa' => 'required'
]);
$products = Product::find($id);
$products->producto = $request->get('producto');
$products->precio = $request->get('precio');
$products->empresa = $request->get('empresa');
$products->save();
return redirect()->route('home')->with('success', 'Data Added');
}
public function destroy($id)
{
$products = Product::findOrFail($id);
$products->delete();
return redirect('home');
}
}
The routes:
Route::get('/', function () {
return view('welcome');
});
//Route::resource('product', 'ProductController');
Auth::routes();
Route::resource('home', 'HomeController');
Route::get('/home', 'HomeController@index')->name('home'); //Vista de usuario
Route::post('/home/sendData', 'HomeController@sendData')->name('home/sendData');
The view you have to go to:
@extends('layouts.app')
@section('content')
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel 5.6 CRUD Tutorial With Example </title>
</head>
<body>
<div class="container">
<h2>Edit A Form</h2><br />
<form method="post" action="{{action('HomeController@update', $id)}}">
<input type="hidden" name="_method" value="PATCH" />
<div class="form-group">
<input type="text" name="producto" class="form-control" value="{{$products['producto']}}" placeholder="Producto" />
</div>
<div class="form-group">
<input type="text" name="precio" class="form-control" value="{{$products['precio']}}" placeholder="Precio" />
</div>
<div class="form-group">
<input type="text" name="empresa" class="form-control" value="{{$products['empresa']}}" placeholder="Empresa" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" value="Edit" />
</div>
</form>
</body>
</html>