Magento 2 – How to Get Data from sales_order_item Table

magento2sales-order

I am new to Magento 2 and
I want to get all data from sales_order_item table in Magento 2.

How to get that can anyone answer?

Best Answer

Try this,

DI Method :

Inject sales order item class in your constructor

 protected $itemFactory;
 public function __construct(
   ....
   \Magento\Sales\Model\Order\ItemFactory $itemFactory,
   ....
 ) {
    ....
    $this->itemFactory = $itemFactory;
    ....
 }

then in your function add the below code to get values from it

 try {    
    $order = $this->itemFactory->create()->getCollection();
    foreach ($order as $items) {
        $items->getItemId();  // similarly you can get all the values from slaes_order_item table
    }
} catch (\Exception $e) {
    error_log($e->getMessage());
}

For more information Follow this answer

Hope this helps.