rudolfbruder's avatar

Laravel collection with only one type of allowed object

Hi,

I am facing a small issue with collections. I am using this method to get data (don't ask me why there is no model in place, was coded by another senior member who is not big fan of eloquent).

    public function getOrdersByIdhdok(array $idHdoks) : Collection
    {
        $rows = $this->getDBConnection()
            ->table('eshop_orders')
            // ->whereIn('idhdok', $idHdoks)
            ->whereIn('id', $idHdoks)
            ->orderBy('id', 'desc')
            ->get();
        foreach ($rows as $row) {
            $orders[] = $this->convertOrderRowToOrderObject($row);
        }
        return collect($orders);
    }

I have added the return collect in there so that i have a collection instead of an array. Now in another class I want to define private method which should return Order object:

    private function getOrderByIdhdok(int $idHdok) : Order
    {
        return $this->currentOrders->where('idhdok', $idHdok)->first();
    }

My problem is that getOrderByIdhdok is supposed to return Order class but as it searches collection and then runs ->first() on it IDE nor the app does not know if the objects in the $this->currentOrders collection are actually of Order type.

Is there any way how to specify that only certain object can be placed in my collection? I can create custom collection class but I am not sure where to define that this collection has only Order objects in it.

0 likes
2 replies

Please or to participate in this conversation.