Problem with DataTable

2

What happens is that when you refresh the page you create a product automatically (it is the last product you enter), that is, you are inserting it when you refresh the page repeatedly, and the other problem is that when you just entered the second product, you It shows the first one in the table, it's as if it's outdated.

Sent the code Thank you!

addInsumos.blade.php TABLE

<div class="panel-body" style="margin-top: 200px;">

                            <table class="table table-bordered" id="myTable">
                                <head>
                                    <th>Id</th>
                                    <th>Nombre</th>
                                    <th>Categoria</th>
                                    <th>fecha</th>
                                    <tbody>
                                        @foreach ($insumos as $insumos )
                                            <tr>
                                                <td>{{$insumos->id}}</td>
                                                <td>{{$insumos->nombre}}</td>
                                                <td>{{$insumos->categoria}}</td>
                                                <td>{{$insumos->created_at}}</td>
                                            </tr>
                                        @endforeach
                                    </tbody>
                                </head>
                            </table>
                        </div>

insumosController.php CONTROLLER

 function viewaddinsumo() 
    {
        //Ver la tabla
        $insumos = \App\insumos::latest('id')
                ->take(5)
                ->get();
        return view('Insumos/addInsumo',compact('insumos'));
    }//                                           _____
                                                 //WW||
    function sendforminsumo(Request $request)   //·.·||
    {      
            //insertar el producto o insumo 
            $insumos = \App\insumos::latest('id')->take(5)->get();
            $name = $request->input('nameP');
            $categoriaP = $request->get('categoriaP');


            DB::table('insumos')->insert(['nombre'=>$name, 'categoria'=>$categoriaP]);

            return view('Insumos/addInsumo',compact('insumos'));


    }
    
asked by Matias Muñoz 19.10.2018 в 14:59
source

1 answer

0

Dear, I think the solution to your query is the way you were saving the elements in your controller .

I recommend that you use Eloquent, to simplify your code.

function sendforminsumo(Request $request)   
    {      
            //insertar el producto o insumo 
            $insumos = new insumos;

            $insumos->name = $request->input('nameP');

            $insumos->categoriaP = $request->get('categoriaP');

            $insumos->save();//guardas el insumo en tu base de datos

            return view('Insumos/addInsumo',compact('insumos'))->with('succes');

    }

I hope you find my answer useful. Greetings

Edit: I also add a documentation of Laravel and specifically that of Eloquent, which will be very helpful. I hope you find it useful.

    
answered by 19.10.2018 / 16:31
source