lundi 28 septembre 2020

Laravel: Using whereBetween to get back Orders is coming as undefined

I store a timestamp as a string and pass it in to getOrders. I am logging $time so I can see it's valid and being logged as a string timestamp.

I am getting undefined returned back but I can't see how. I am using count($paidOrders) > 0 ? array_values($customers) : [] in the return to protect me from getting back an empty value so that if there are no $paidOrders, I get back an empty array of [], which I would expect if it queries and gets not matching orders within those times.

public function getOrders($time)
{
    try {
        // $time is a timestamp in string format; ex: "1601339962"

        $paidOrders = Order::whereBetween('orders.created_at', [ Carbon::createFromTimestamp($time) , Carbon::now()])->get()->all();
        $customers = [];

        if (count($paidOrders) > 0) {
            foreach ($paidOrders as $paidOrder) {
                if(!isset($customers[$paidOrder->customer_id])) {
                    $customers[$paidOrder->customer_id]['customer_name'] = $paidOrder->ship_name;
                    $customers[$paidOrder->customer_id]['customer_id'] = $paidOrder->customer_id;
                    $customers[$paidOrder->customer_id]['total'] = $paidOrder->subtotal;
                }
                else{
                    $customers[$paidOrder->customer_id]['total'] += $paidOrder->subtotal;
                }
                $customers[$paidOrder->customer_id]['orders'][] = $paidOrder->id;
            }
        }

        return [
            'paidOrdersDuringStream' =>  count($paidOrders) > 0 ? array_values($customers) : [],
        ];
    }


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire