mightyteja
2 months ago

How to remove null from the array while inserting to database

Posted 2 months ago by mightyteja

Here is my array,

"sku" => array:3 [▼
    0 => "zxys"
    1 => null
    2 => "1234"
  ]

"product_id" => array:3 [▼
    0 => "1"
    1 => "3"
    2 => "1"
  ]

I am trying to remove the value 1 where it has null value.

Here is my Controller logic,

foreach ($request->sku as $item => $v) {
            $data = array(
                'sku' => $request['sku'][$item],
		'product_id' => $request['product_id'][$item]
            );

            $filter = array_filter($data, function ($value) {
                return $value !== null;
            });

            sku::insert($filter);
        }

While I try to filter the array result to remove the null, the database has entry of the null.

Thanks in advance.

Please sign in or create an account to participate in this conversation.