lifesound's avatar

Get all shipments and foreach shipment i need to count each products

I want to get all the shipments adding to each shipment each count of the scanned prods and the unscanned prods as shown here :


    public function allShipments()
    {
        $ships = Shipment::all();
        foreach ($ships as $ship) {
            $scanned_products = $ship->products()->where('scanned', true)->where('visibility',true)->get();
            $unscanned_products = $ship->products()->where('scanned', false)->where('visibility',true)->get();
            $ship->$scanned_products = $scanned_products;
            $ship->$unscanned_products = $unscanned_products;
        }
        return Datatables::of($ships)->make();
    }

I want only the count of scanned and unscanned products foreach every ship , then using datatables with them which obligates not using all() or get as it only needs the query itself.

0 likes
14 replies
lifesound's avatar

@MichalOravec Not really there are some conditionals that I need. I see in the docs some conditionals BUT I am not able to generate the full query required for my case

lifesound's avatar
    public function allShipments()
    {
        $ships = DB::table('shipments');
        foreach ($ships as $ship){
            $ship->scanned = DB::table('products')->where('shipment_id',$ship->id)->where('scanned', true)->count();
            $ship->unscanned = DB::table('products')->where('shipment_id',$ship->id)->where('scanned', false)->count();
        }
        return Datatables::of($ships)->make();
    }

also this does not work errors

lifesound's avatar

no dice here as well

    public function allShipments()
    {
        $ships = DB::table('shipments');

        foreach ($ships as $ship){
            $ship->scanned = Shipment::withCount('products')->where('scanned', true);
            $ship->unscanned = Shipment::withCount('products')->where('scanned', false);
        }
        
        return Datatables::of($ships)->make();
    }
lifesound's avatar

i have this err on the console

{message: "The route [object%20Object] could not be found.",…}
exception
: 
"Symfony\Component\HttpKernel\Exception\NotFoundHttpException"
file
: 
/vendor/laravel/framework/src/Illuminate/Routing/AbstractRouteCollection.php"
line
: 
44
message
: 
"The route [object%20Object] could not be found."
trace
:
lifesound's avatar

I did it like that manually and it is ok

        $ships =Shipment::all();
        foreach ($ships as $ship){
            $ship->scanned =  Product::where('shipment_id',$ship->id)->where('scanned',true)->count();
            $ship->unscanned =  Product::where('shipment_id',$ship->id)->where('scanned',false)->count();
        }

MichalOravec's avatar
Level 75

@lifesound It was hard to see an example in the documentation, wasn't it?

$ships = Shipment::withCount([
    'products as scanned' => function ($query) {
        $query->where('scanned', true);
    },
    'products as unscanned' => function ($query) {
        $query->where('scanned', false);
    },
])->get();

or

$ships = Shipment::withCount([
    'products as scanned' => fn ($query) => $query->where('scanned', true),
    'products as unscanned' => fn ($query) => $query->where('scanned', false)
])->get();

Please or to participate in this conversation.