How to insert data in a specific table by observer file in Magento 2

If you want to insert data in a specific table by observer on Magento 2 website, follow these steps:How to insert data in a specific table by observer file in Magento 2/span>

1. First of all add the constructor in your observer class code:

public function __construct(
        \Magento\Framework\App\ResourceConnection $resourceConnection
    )
    {
        $this->resourceConnection = $resourceConnection;
    }


2. Then, add the following code in your observer execute function code:

$connection  = $this->resourceConnection->getConnection();

 $tableName = $this->resourceConnection->getTableName('aitoc_mli_order_item_warehouse');


 $order = $this->orderRepository->get($orderId);
    $item_data = [];
    $count = 0;
    foreach ($order->getAllVisibleItems() as $item) {

        $item_data[$count]['item_id'] = $item->getItemId();
        $item_data[$count]['warehouse_id'] = $item->getWarehouseId();
        $item_data[$count]['product_id'] = $item->getProductId(); //for product Id
        if($item->getWarehouseId()) {
            $item_data[$count]['warehouse_id'] = $item->getWarehouseId();
            $review = [
                        'order_item_id' => $item_data[$count]['item_id'],
                        'warehouse_id' => $item_data[$count]['warehouse_id'],
                        'qty' => 0
                    ];
            $this->resourceConnection->getConnection()->insert($tableName, $review);
        }
        $count++;
    }

How to insert data in a specific table by observer file in Magento 2expoundsofttechsolution

How to get the current URL of Laravel in controller and view?