Magento 2 Left Join Collection Query


Magento 2 Left Join Collection Query ->joinLeft() is used for left join with collection in magento 2. It is very simple to use ->joinLeft(), left join returns all data from left table and the matching records from right table. Here in this tutorial we are going to create very simple and basic example of left join in Magento 2.


Magento 2 Left Join Collection Query | Example

Here we have created an example for left join with order table and order_item table, which will pull the data based on left join query-

Magento 2 Left Join Collection Query | Example:

$objectManager =  \Magento\Framework\App\ObjectManager::getInstance();
$collection = $objectManager->get('Magento\Sales\Model\Order')->getCollection();
$collection->getSelect()
->joinLeft( array('order_item'=> 'sales_order_item'), 'order_item.order_id = main_table.entity_id', array('order_item.sku')); 

On the same way you can use left join with multiple tables.


Advertisements

Add Comment

📖 Read More