function store(Request $request) { $input = $request->all(); $testArr = array(); $getParam = Parameter::where('name', '=', $input['parameter'])->first(); if (!empty($getParam)) { $testArr['parameter_id'] = $getParam->id; } else { $parameter = new Parameter(); $parameter->create(array('name' => $input['parameter'])); $testArr['parameter_id'] = DB::getPdo()->lastInsertId(); } $getState = Parameter::where('name', '=', $input['state'])->first(); if (!empty($getState)) { $testArr['state_id'] = $getState->id; } else { $state = new State(); $state->create(array('name' => $input['state'])); $testArr['state_id'] = DB::getPdo()->lastInsertId(); } $getMethod = Method::where('name', '=', $input['method'])->first(); if (!empty($getMethod)) { $testArr['method_id'] = $getMethod->id; } else { $method = new Method(); $method->create(array('name' => $input['method'])); $testArr['method_id'] = DB::getPdo()->lastInsertId(); } $getTestMethod = TestMethod::where('name', '=', $input['test_method'])->first(); if (!empty($getTestMethod)) { $testArr['test_method_id'] = $getTestMethod->id; } else { $tmethod = new TestMethod(); $tmethod->create(array('name' => $input['test_method'])); $testArr['test_method_id'] = DB::getPdo()->lastInsertId(); } $checkTest = Test::where('parameter_id', '=', $testArr['parameter_id'])->where('parameter_id', '=', $testArr['state_id'])->where('parameter_id', '=', $testArr['method_id'])->where('parameter_id', '=', $testArr['test_method_id'])->first(); if ($checkTest) { $test_id = $checkTest->id; Session::flash('flash_message', 'Test already exist.'); } else { $testArr['price'] = 147; $test = new Test(); $test::create($testArr); $test_id = DB::getPdo()->lastInsertId(); Session::flash('flash_message', 'Test created successfully.'); } if ($test_id) { DB::table('test_processes')->where('test_id', '=', $test_id)->delete(); if ($request->has('test_item')) { $items = $request->get('test_item'); foreach ($items as $key => $value) { $process = new Process(); $process::create(array('test_id' => $test_id, 'item_id' => $value, 'status' => 1)); } } } Session::flash('flash_type', 'alert-success'); return redirect('admin/tests'); }
function update(Request $request, $id) { $input = $request->all(); $files = $request->file('test_files'); if (!empty($files[0])) { foreach ($files as $file) { $fileName = $file->getClientOriginalName(); $file->move(public_path() . '/TestFiles/' . $id, $fileName); $file = new TestFile(); $file::create(array('file_name' => $fileName)); } } $getParam = Parameter::where('name', '=', $input['parameter'])->first(); if (!empty($getParam)) { $arr['parameter_id'] = $getParam->id; } $getState = Parameter::where('name', '=', $input['state'])->first(); if (!empty($getState)) { $arr['state_id'] = $getState->id; } $getMethod = Method::where('name', '=', $input['method'])->first(); if (!empty($getMethod)) { $arr['method_id'] = $getMethod->id; } $getTestMethod = TestMethod::where('name', '=', $input['test_method'])->first(); if (!empty($getTestMethod)) { $arr['test_method_id'] = $getTestMethod->id; } $test = Test::find($id); $test->update($arr); // update test items // DB::table('test_processes')->where('test_id', '=', $id)->delete(); if ($request->has('test_item')) { $items = $request->get('test_item'); foreach ($items as $key => $value) { $process = new Process(); $process::create(array('test_id' => $id, 'item_id' => $value, 'status' => 1)); } } Session::flash('flash_message', 'Test has been updated successfully.'); Session::flash('flash_type', 'alert-success'); return redirect('admin/tests'); }