You need to think more like class, name space and functions so what you like have to go inside a function, in one of your modules:
also try to learn about ordercollectionfactory. here is an example
<?php
namespace 'moduleNameSpace';
class ModelClass extends \Magento\Framework\View\Element\Template
{
protected $_orderCollectionFactory;
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory,
array $data = []
) {
$this->_orderCollectionFactory = $orderCollectionFactory;
parent::__construct($context, $data);
$this->_isScopePrivate = true;
}
protected function _construct()
{
parent::_construct();
$this->_orderCollectionFactory->create()->addAttributeToSelect('*')
}
public function getmyorderdetails(array $filters = [])
{ $orders = $this->orderCollectionFactory->create();
$orderitem=$orders->addFieldToFilter(('name',$filters);
$ids =array();
foreach($orderitem as $item){
$ids[] = $item->getOrderId();
}
$orders->addFieldToFilter("entity_id",array("in",$ids));
return $this->orderCollectionFactory;
}
}