Hi guys,
I'm wondering how to deal with this situation:
I have this structure:
Ingredients (basic list of ingredients)
Groceries (basic list of groceries)
Recipe (recipe which is made from ingredients and/or groceries)
Recipeables (pivot table)
recipeable is FQN to an ingredient or grocery
I have this panel:
I'm trying to figure out best code solution, here is what I have so far, but I still have little feeling, that there is a better solution.
// Service
class AddRecipeableToRecipeService
{
/**
* @param Recipe $recipe
* @param RecipeableRequest $request
*/
public function add(Recipe $recipe, RecipeableRequest $request)
{
$recipeable = Helper::getRecipeableModel(
$request->input('recipeable'),
$request->input('type')
);
$value = (int) $request->input('value');
$order = (int) $request->input('order');
$attributes = compact('value', 'order');
if ($recipeable->hasMacronutrients() === TRUE) {
$attributes += $recipeable->ratio($value);
}
// type is "recipe" or "grocery"
$recipe->{str_plural($request->input('type'))}()->attach($recipeable, $attributes);
}
}
// Helper
class Helper
{
/**
* @var string[]
*/
private static $model = [
'ingredient' => Ingredient::class,
'grocery' => Grocery::class,
];
/**
* @param int $ID
* @param string $type
* @return Ingredient|Grocery
*/
public static function getRecipeableModel($ID, $type)
{
return app(self::$model[$type])->findOrFail($ID);
}
}
// Remove example
DB::table('recipeables')->delete($id);
What do you think? I don't know, how to work with editing, I need pivot values and model too.
Thanks a lot
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community