Tuesday, March 7, 2017

PHP/Laravel - Function empty on variable but if I return the method has data inside foreach

foreach($parcels as $parcel):
        $length  = $helper->convertToCM($parcel->length, $parcel->distance->name);
        $width   = $helper->convertToCM($parcel->width, $parcel->distance->name);
        $height  = $helper->convertToCM($parcel->height, $parcel->distance->name);
        $weight  = $helper->convertToKG($parcel->weight, $parcel->mass->name);

        return array(
            "length"        => $length,
            "width"         => $width,
            "height"        => $height,
            "weight"        => $weight,
            "distance_unit" => env('SHIPPO_DISTANCE_UNIT'),
            "mass_unit"     => env('SHIPPO_MASS_UNIT')
        );

result

{
 "length": {},
 "width": {},
 "height": {},
 "weight": {},
 "distance_unit": "cm",
 "mass_unit": "kg"
}

But if I do (inside the foreach)

return $length

I get the expected result... Must be something stupid but I'm not seeing what



via Bruno Teixeira

Advertisement