jeudi 1 décembre 2016

Laravel eloquent where returns result only for first element in foreach?

I'm having a simple hasMany relation between order and orderItems. What I'm trying to do is fetch the count of similar order items. This is what I've done:

        $orderItems = $order->orderItems();

        $distinctItems = $orderItems->groupBy('item_name')->distinct('item_name')->get();


        foreach($distinctItems as $i){

            $i['count'] = $orderItems->where('item_name', '=', $i->item_name)->count();
        }

        $order['items'] = $distinctItems;

However the count is returned for only first order Item and 0 for other items. I'm not sure why this is happening. I've also checked that where() returns null for the items except the first one.

Thanks for the help.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2gsU9TO
via IFTTT

Aucun commentaire:

Enregistrer un commentaire