Home  >  Q&A  >  body text

SQLSTATE: Integrity constraint violation: 1048 Column 'user_id' cannot be null

I get this error when I try to insert an object in laravel, this is my function in the controller class:

public function insertDelete(Producto $item)
{
     $date = date('Y-m-d');
   $venta=new venta();

     $venta->fechaVenta=$date;
     $venta->user_id=$item->user_id;
    $venta->producto_id=$item->id;
     $venta->envio_id=$item->envio_id;
    $venta->save();
    Producto::where('id',$item->id)->delete();
    return view('pruebas');}

My route:

@foreach ($ticket as $item)<div class="d-flex my-2 justify-content-center">
      <a href="{{ route('insertDelete' , $item) }}" class="btn btn-success btn-label waves-effect right waves-light rounded-pill"><i class="ri-check-double-line label-icon align-middle rounded-pill fs-16 ms-2"></i> Finalizar Compra</a>
  </div>@endforeach

My web.php

Route::get('pruebas/', [\App\Http\Controllers\VentaController::class, 'insertDelete'] )->name('insertDelete');

P粉576184933P粉576184933185 days ago328

reply all(1)I'll reply

  • P粉482108310

    P粉4821083102024-03-22 17:57:26

    In your route, you do not specify the parameters of the route model binding. You are expecting $item , in which case you want something like this:

    Route::get('pruebas/{$item}', [\App\Http\Controllers\VentaController::class, 'insertDelete'] )->name('insertDelete');

    reply
    0
  • Cancelreply