/** * Transform food response * @param Food $food * @return array */ public function transform(Food $food) { $array = ['id' => $food->id, 'name' => $food->name, 'path' => $food->path, 'defaultCalories' => $food->getDefaultCalories(), 'unitIds' => $food->units()->lists('unit_id')]; /** * @VP: * I'm doing this here so I can include the units from my IngredientTransformer. * What's the proper way of doing this? * Actually, $array['units'] is an empty array here. Why? * It should be populated, and if I dd what includeUnits returns, * the data looks correct. */ if (isset($this->params['units'])) { $array['units'] = $this->includeUnits($food); } // if ($food->default_unit_id) { // $array['defaultUnit'] = [ // 'id' => $food->defaultUnit->id, // 'name' => $food->defaultUnit->name // ]; // $array['defaultUnit'] = $food->defaultUnit; // $array['defaultUnit'] = $this->includeDefaultUnit($food); // } return $array; }