Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mprythero's avatar

Count Array where Column Value Equals Given

I have a controller where an array of checkbox values are passed on to the controller and then sent on to do a variety of tasks. My problem is focused on getting a count of the arrays' shipment's customer's billing method, but I'm not entirely sure how I would get those values since I'm posting an array and I want a count.

Here's my controller:

    public function sendNow(Request $request)
        {
            $now = Carbon::now();
            $sendNowShipment = array();
            $method = array();
            $sendNowShipment = request('send');
                foreach($sendNowShipment as $SNS){
                    $shipment = Shipment::findOrFail($SNS);
                    if($shipment->billtoAccount->billingMethods->label == "Mail"){
                    $timestamp = Carbon::now()->timestamp;
                    $shipment_details = $shipment->shipment_details;
                        $path = 'temp/freightbill_'.$shipment->pro_number.'-'.$timestamp.'.pdf';
                        $sessionPath[] = $path;
                        $pdf = PDF::loadView('shipments.pdf', compact('shipment', 'shipment_details'))
                            ->save($path);
                    }elseif($shipment->billtoAccount->billingMethods->label == "Email"){
                        $billToAccount =   $shipment->billtoAccount;
                        $billToAccountUsers = $billToAccount->users;
                            if ($billToAccount->primary_email){
                                $billToEmail[] = $billToAccount->primary_email;
                            }
                            if ($billToAccountUsers->count() > 0){
                                foreach ($billToAccountUsers as $billToAccountUser){
                                    $billToEmail[] = $billToAccountUser->email;
                                }
                            }
                            foreach ($billToEmail as $bte){
                                Mail::to($bte)->send(new newBillToShipment($shipment));
                            }
                    }
                }
            
            if(count($shipment->billtoAccount->billingMethods->label == "Mail") > 0){// count where shipments->customers->billingMethods = Mail) > 0
                $pdf = new PDFMerger();
    
                // Add 2 PDFs to the final PDF
                foreach($sessionPath as $sp){
                $pdf->addPDF($sp, 'all');
                }
    
                // Merge the files and retrieve its PDF binary content
                $timestamp = Carbon::now()->timestamp;
                $binaryContent = $pdf->merge('download', $timestamp."-printBatch.pdf");
    
                // Return binary content as response
                //return response($binaryContent)
                  //  ->header('Content-type' , 'application/pdf');
                return back();
                //dd($sendNowShipment,$sendMethod);
        }
        }

If you look at this line (a little past the middle):

    if(count($shipment->billtoAccount->billingMethods->label == "Mail") > 0){
    // count where shipments->customers->billingMethods = Mail) > 0

You'll see that I'm going through a multitude of relationships just to get the value of "Mail" that I am looking for.

So I'm wondering if this should be a DB:: query through laravel, but I'm not sure how I would perform the query using the given checkbox array I have being POSTed to this controller.

0 likes
0 replies

Please or to participate in this conversation.