Get Shipment collection details by order id Magento 2.

You can get the Shipment data or collection for the specific order by order id in Magento 2.

Using Magento\Sales\Api\ShipmentRepositoryInterface interface, you need to use getList() function to fetch no. of Shipment by sales order id.

Using SearchCriteriaBuilder Class, You need to use addFilter() method, filter by order id and pass searchCriteria object to getList() method of ShipmentRepositoryInterface.

<?php
namespace Path\To\Class;

use Exception;
use Psr\Log\LoggerInterface;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Sales\Api\ShipmentRepositoryInterface;

class ShipmentDataByOrder
{
    /**
     * @var ShipmentRepositoryInterface
     */
    private $shipmentRepository;

    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;

    /**
     * @var LoggerInterface
     */
    private $logger;

    public function __construct(
        ShipmentRepositoryInterface $shipmentRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        LoggerInterface $logger
    ) {
        $this->shipmentRepository = $shipmentRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->logger = $logger;
    }

    /**
     * Shipment by Order id
     *
     * @param int $orderId
     * @return ShipmentInterface[]|null |null
     */
    public function getShipmentDataByOrderId(int $orderId)
    {
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('order_id', $orderId)->create();
        try {
            $shipments = $this->shipmentRepository->getList($searchCriteria);
            $shipmentRecords = $shipments->getItems();
        } catch (Exception $exception)  {
            $this->logger->critical($exception->getMessage());
            $shipmentRecords = null;
        }
        return $shipmentRecords;
    }
}

Using the above way you got the output as Shipment records as an array and iterate over a loop to Shipment array to fetch no. of different Shipment record for an order.

$orderId = 1; // order id

$shipments = $block->getShipmentDataByOrderId($orderId);
foreach ($shipments as $shipment) {
    # code...
    echo "<pre>";print_r($shipment->getData());
}

Output:

Array
(
    [entity_id] => 1
    [store_id] => 1
    [total_weight] => 
    [total_qty] => 5.0000
    [email_sent] => 
    [send_email] => 
    [order_id] => 1
    [customer_id] => 1
    [shipping_address_id] => 1
    [billing_address_id] => 2
    [shipment_status] => 
    [increment_id] => 000000001
    [created_at] => 2019-08-17 12:19:20
    [updated_at] => 2019-08-17 12:19:20
    [packages] => Array
        (
        )

    [shipping_label] => 
    [customer_note] => 
    [customer_note_notify] => 
    [extension_attributes] => Magento\Sales\Api\Data\ShipmentExtension Object
        (
            [_data:protected] => Array
                (
                    [source_code] => default
                )

        )

)