Здравствуйте, подскажите пожалуйста, есть у меня два таких условия:
if($request->filled('size')) {
$idSize = HelperProduct::getId($request->input('size'));
$priceSize = HelperProduct::getPrice($request->input('size'));
$sizesObject = Material::whereIn('id',$idSize)->get();
$product->sizes()->attach($idSize);
foreach ($priceSize as $value) {
foreach ($sizesObject as $size) {
if($value['id'] == $size['id']) {
$price = new Price();
$price->store(['price' => $value['value'],'product_id' => $product->id]);
$price->entity()->associate($size)->save();
}
}
}
}
if($request->filled('material')) {
$idMaterial = HelperProduct::getId($request->input('material'));
$priceMaterial = HelperProduct::getPrice($request->input('material'));
$materialObject = Size::whereIn('id',$idMaterial)->get();
$product->materials()->attach($idMaterial);
foreach ($priceMaterial as $value) {
foreach ($materialObject as $material) {
if($value['id'] == $material['id']) {
$price = new Price();
$price->store(['price' => $value['value'],'product_id' => $product->id]);
$price->entity()->associate($material)->save();
}
}
}
}
Как можно сократить повторения ?
Заранее благодарю за ответ.