search

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:

1

2

3

4

5

6

7

8

9

10

11

12

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:

1

2

3

@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

1

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

P粉576184933P粉576184933371 days ago476

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:

    1

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

    reply
    0
  • Cancelreply