como no logre usar datatables, logré improvisar un método diferente. Uno que abre un formulario que edita las tablas, el de borrar ya que esta hecho, pueden ignorar ese.
Por alguna razon, la view no aparece. Dice error 404.
La vista en 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>
El 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');
}
}
Las rutas:
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');
La view a la que tiene que ir:
@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>