only(['name', 'description']); $ingredients = $data->get('ingredients'); $steps = $data->get('steps'); $recipe = Recipe::create($recipeData->toArray()); foreach ($ingredients as $ingredient) { RecipeProduct::create([ 'recipe_id' => $recipe['id'], 'product_id' => $ingredient['id'], 'weight' => $ingredient['weight'] ]); } foreach ($steps as $step) { RecipeStep::create(['recipe_id' => $recipe['id'], 'step' => $step]); } return $this->get($recipe['id']); }); } public function get(int $id) { $rawRecipe = Recipe::where('id', $id)->first(); $recipe = [ 'name' => $rawRecipe['name'], 'description' => $rawRecipe['description'], 'ingredients' => $this->processIngredients($rawRecipe['ingredients']), 'steps' => $rawRecipe['steps']->pluck('step') ]; return $recipe; } public function processIngredients(Collection $ingredients) { return $ingredients->map(function ($item) { return collect(array_merge($item->toArray(), $item['product']->toArray())) ->only(['name', 'weight']); }); } public function paginate($chunkSize) { return Recipe::paginate($chunkSize); } }