public function afterSave() { $retorno = parent::afterSave(); if (!$this->isNewRecord) { return $retorno; } if (isset($this->_produtos)) { foreach ($this->_produtos as $produto) { $modelProdutoPedido = new ProdutoPedido(); $modelProdutoPedido->attributes = $produto; $modelProdutoPedido->pedido_id = $this->id; $modelProdutoPedido->save(); } } if (isset($this->_pizzas)) { foreach ($this->_pizzas as $pizza) { $modelPizza = new Pizza(); $modelPizza->attributes = $pizza; $modelPizza->pedido_id = $this->id; $modelPizza->_sabores = $pizza['sabores']; $modelPizza->_adicionais = isset($pizza['adicionais']) ? $pizza['adicionais'] : null; $modelPizza->save(); } } if (isset($this->_combinados)) { foreach ($this->_combinados as $combinado) { $modelCombinadoPedido = new CombinadoPedido(); $modelCombinadoPedido->preco = $combinado['preco']; $modelCombinadoPedido->combinado_id = $combinado['combinado_id']; $modelCombinadoPedido->pedido_id = $this->id; $modelCombinadoPedido->_item_combinado = $combinado['itens_combinado']; $modelCombinadoPedido->save(); } } $retorno = false; return $retorno; }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { // $pizza = new Pizza(); $order_id = Input::get('order'); $order = Order::find($order_id); $order->save(); $pizza_name = Input::get('pizza_name'); if ($pizza_name) { $pizza->pizza_name = $pizza_name; } else { $pizza->pizza_name = "N/A"; } $pizza->amount = 0; $pizza->total = 0; $quantity = Input::get('quantity'); $pizza->quantity = $quantity; $pizza->save(); $size = Input::get('size'); if ($size == 'solo') { $pizza->size = $size; $base = Input::get('base'); if ($base) { $pizza->ingredients()->attach($base); } else { return Redirect::to('/pizza/create')->with(array('order' => $order->order_id, 'message' => "It ain't pizza if there ain't no base!"))->withInput(Input::only('pizza_name', 'quantity', 'size')); } $sauce = Input::get('sauce'); $pizza->ingredients()->attach($sauce); $cheese = Input::get('cheese'); $pizza->ingredients()->attach($cheese); $meats = Input::get('meats'); if (sizeof($meats) != 0) { foreach ($meats as $meat) { $pizza->ingredients()->attach($meat); } } $chilis = Input::get('chilis'); if (sizeof($chilis) != 0) { foreach ($chilis as $chill) { $pizza->ingredients()->attach($chill); } } $toppings = Input::get('toppings'); if (sizeof($toppings) != 0) { foreach ($toppings as $topping) { $pizza->ingredients()->attach($topping); } } } else { if ($size == 'large') { $pizza->size = $size; $base = Input::get('base2'); $pizza->ingredients()->attach($base); $sauce = Input::get('sauce2'); $pizza->ingredients()->attach($sauce); $cheese = Input::get('cheese2'); $pizza->ingredients()->attach($cheese); $meats = Input::get('meats2'); if (sizeof($meats) != 0) { foreach ($meats as $meat) { $pizza->ingredients()->attach($meat); } } $chilis = Input::get('chilis2'); if (sizeof($chilis) != 0) { foreach ($chilis as $chill) { $pizza->ingredients()->attach($chill); } } $toppings = Input::get('toppings2'); if (sizeof($toppings) != 0) { foreach ($toppings as $topping) { $pizza->ingredients()->attach($topping); } } } } if (sizeof($pizza->ingredients) <= 4) { return Redirect::to('/pizza/create')->with(array('order' => $order->order_id, 'message' => "Too few ingredients. Should have at least 4."))->withInput(Input::only('pizza_name')); } else { $amount = 0; foreach ($pizza->ingredients as $ingr) { $amount = $amount + $ingr->price; } //price of the individual pizza $pizza->amount = $amount; //total amount = price of pizza * quantity $total = $amount * $quantity; $pizza->total = $total; $pizza->save(); $order->pizzas()->attach($pizza); $order->save(); //Show newly created pizza return Redirect::to('/order/' . $order->order_id . ''); } }