/** * Creates a new Material model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Material(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
public function store(MaterialRequest $request) { try { DB::transaction(function () use($request) { $material = new Material(); $material->name = $request->input('name'); $material->type = $request->input('type'); $material->status = $request->input('status'); $material->created_by = Auth::user()->id; $material->created_at = time(); $material->save(); $insertedId = $material->id; // Current Year Opening Stock $rawStock = new RawStock(); $rawStock->material_id = $insertedId; $rawStock->year = CommonHelper::get_current_financial_year(); $rawStock->stock_type = Config::get('common.balance_type_opening'); $rawStock->created_by = Auth::user()->id; $rawStock->created_at = time(); $rawStock->save(); // Current Year Intermediate Stock $rawStock = new RawStock(); $rawStock->material_id = $insertedId; $rawStock->year = CommonHelper::get_current_financial_year(); $rawStock->stock_type = Config::get('common.balance_type_intermediate'); $rawStock->created_by = Auth::user()->id; $rawStock->created_at = time(); $rawStock->save(); }); } catch (\Exception $e) { Session()->flash('error_message', 'Material not created!'); return redirect('materials'); } Session()->flash('flash_message', 'Material has been created!'); return redirect('materials'); }