Armani
3 months ago
692
3
Laravel

How to refactor this code?

Posted 3 months ago by Armani

I have this code to insert multiple rows to table:


rest = Restaurant::create($this->check());
$items = [];
if ($rest) {
 foreach (request()->item_id as $key => $value) {
  $items[] = [
'item_id' => request()->item_id[$key],
'type_id' => request()->type_id[$key],
'brand' => request()->brand[$key],
'quantity' => request()->quantity[$key],
'price' => request()->price[$key],
];
}
 $rest->details()->createMany($items);
} 

And it works fine but I want to refactor it, using Collection or other methods. but I couldn't.

I have 5 inputs inside blade file with names: item_id[], type_id[], brand[], quantity[] and price[].

Any suggestions?

Please sign in or create an account to participate in this conversation.