Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Alewa's avatar
Level 2

Undefined array key "id" Laravel error

I am having an Undefined array key "id" error when i try to add a product to cart, any help? web.php

Route::get('orders', [\App\Http\Controllers\Admin\OrderController::class, 'index']);
Route::get('new-order', [\App\Http\Controllers\Admin\ProductController::class, 'newOrder']);
Route::post('/addcart/{id}', [App\Http\Controllers\Admin\ProductController::class, 'addToCart']);

products database table

$table->id();
            $table->string('product_name');
            $table->string('measurement_unit')->nullable();  //this is the measurement unit eg. 5 g, 3 kg, pair
            $table->double('cost_price', 8, 2);  //this is how much you bought the medicine at wholesale price.
            $table->double('sales_price', 8, 2);  //this is how much you are selling your medicine
            $table->integer('stock'); //amount of product or quantity in stock
            $table->longText('description')->nullable();
            $table->boolean('status')->default(1);
            $table->mediumText('product_image')->nullable();
            $table->integer('storage_id')->foreign('storage_id')->references('id')->on('storages')->onDelete('cascade')->nullable();
            $table->integer('brand_id')->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade')->nullable();
            $table->integer('category_id')->foreign('category_id')->references('id')->on('categories')->onDelete('cascade')->nullable();
            $table->integer('admin_created_id')->foreign('admin_created_id')->references('id')->on('admins')->onDelete('cascade')->nullable();
            $table->integer('admin_updated_id')->foreign('admin_updated_id')->references('id')->on('admins')->onDelete('cascade')->nullable();
            $table->timestamps();

Product.php Model

public static function getProductStock($id){
        $getProductStock = Product::select('stock')->where(['id'=>$id])->first();
        return $getProductStock->stock;
    }

ProductController

public function addToCart(Request $request)
    {
        if($request->isMethod('post')){
            $data = $request->all();
            //echo"<pre>"; print_r($data); die;
    
            //Check if product stock is available or not
            $getProductStock = Product::getProductStock($data['id']);
            if($getProductStock<$data['quantity']){
              Alert::warning('Required Quantity is not available!');
              return back();
            }
    
            //Generate Session Id at cart table if not exist and if user has not login
            $session_id = Session::get('session_id');
            if(empty($session_id)){
              $session_id = Session::getId();
              Session::put('session_id',$session_id);
            }
    
            //If product already exist in cart table
            if(Auth::check()){
              //if user is login
              $session_id = "";
              $admin_created_id = Auth::guard('admin')->user()->id;
              $countProducts = Cart::where(['product_id'=>$data['product_id'],'admin_created_id'=>$admin_created_id])->count();
            }else{
              //If user is not login
              $user_id = 0;
              $countProducts = Cart::where(['product_id'=>$data['product_id'],'session_id'=>$session_id])->count();
            }
    
            if($countProducts>0){
              Alert::warning('Product Already exist in cart!');
              return back();
            }
    
            //Add products to cart table
            $item = new Cart;
            $item->session_id = $session_id;
            $item->admin_created_id = $admin_created_id;
            $item->product_id = $data['product_id'];
            $item->quantity = $data['quantity'];
            $item->save();
    
            Alert::success('Product Added to cart successfully! <a href="/cart">View Cart</a>');
            return back();
          }
    }

And i am having error on the ProductController $getProductStock = Product::getProductStock($data['id']);, any help?

0 likes
2 replies
jdc1898's avatar

It doesn't look like you are checking if $data is empty. I would recommend that you run the request through validation before moving forward.

Snapey's avatar

you are passing the id in the route;

    public function addToCart(Request $request, $id)
    {
            //Check if product stock is available or not
            $getProductStock = Product::getProductStock($id);

Also, you don't need to check if its a post request. The router already did that for you

Please or to participate in this conversation.