Пример #1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     // Validate entries
     // No entries to validate for a CV.
     //var_dump($request);
     //TODO change firstOr*** to ***
     // Create the CV first.
     $cv = $request->user()->cvs()->firstOrCreate(['name' => $request->name]);
     // Update the user's data.
     $request->user()->update(['surname' => $request->surname, 'lastname' => $request->lastname, 'address' => $request->address, 'telephone' => $request->telephone]);
     // Build the subsections first.
     $work = $request->user()->jobs()->firstOrNew(['name' => $request->enterprise, 'location' => $request->enterpriseAddress, 'title' => $request->job_name, 'description' => 'not filled yet', 'start_date' => $request->enterpriseBeginDate, 'end_date' => $request->enterpriseEndDate]);
     $education = $request->user()->educations()->firstOrNew(['name' => $request->school, 'location' => $request->schoolAddress, 'title' => $request->degree, 'description' => 'not filled yet', 'start_date' => $request->degreeBeginDate, 'end_date' => $request->degreeEndDate]);
     $language = $request->user()->languages()->firstOrNew(['name' => $request->language, 'level' => $request->languageLevel, 'creditation' => $request->languageDegree]);
     // New build the sections.
     $workSection = new Section(['type' => 'work']);
     $languageSection = new Section(['type' => 'language']);
     $educationSection = new Section(['type' => 'education']);
     // First attach the sections to the user
     $request->user()->sections()->saveMany([$workSection, $educationSection, $languageSection]);
     // Now attach them to the cv
     $cv->sections()->saveMany([$workSection, $educationSection, $languageSection]);
     // Now attach each section with it's correct subsection.
     $languageSection->language()->save($language);
     $language->save();
     $workSection->job()->save($work);
     $work->save();
     $educationSection->education()->save($education);
     $education->save();
     // Now save the sections.
     $workSection->save();
     $educationSection->save();
     $languageSection->save();
     return redirect('/cvs');
 }