Last active
September 9, 2018 20:46
-
-
Save DreddyI/c37728709866efdc491fb37686d1c9ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * by dreddyi | |
| * Date: 12.08.18 | |
| * Time: 4:12 | |
| */ | |
| namespace App\Helpers; | |
| use App\Models\Child; | |
| class ChildHelper | |
| { | |
| public static function createNewChild($_child) | |
| { | |
| $child = Child::where(['name' => $_child['name']])->first(); | |
| if ($child === null) { | |
| $child = Child::create(['name' => $_child['name']]); | |
| } | |
| return $child; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * by dreddyi | |
| * Date: 12.08.18 | |
| * Time: 4:10 | |
| */ | |
| namespace App\Http\Controllers; | |
| use App\Helpers\ChildHelper; | |
| use App\Helpers\TutorHelper; | |
| use App\Models\Exercise; | |
| use Illuminate\Http\Request; | |
| class ExerciseStoreController | |
| { | |
| public function create(Request $request) | |
| { | |
| $exercise = new Exercise([ | |
| 'title' => $request->title, | |
| 'description' => $request->description, | |
| 'exercise_time' => date('Y-m-d H:i:s', strtotime($request->exercise_time)), | |
| 'payment' => $request->payment, | |
| ]); | |
| $exercise->save(); | |
| $this->appendChild($exercise, $request); | |
| $this->appendTutors($exercise, $request); | |
| $this->appendPreparers($exercise, $request); | |
| $exercise->load('child', 'tutors', 'preparers'); | |
| return response()->json(compact('exercise'), 200); | |
| } | |
| public function delete(Request $request, Exercise $exercise) | |
| { | |
| $exercise->delete(); | |
| return response()->json([], 200); | |
| } | |
| private function appendTutors(Exercise $exercise, Request $request) | |
| { | |
| $tutorsIds = $request->tutors; | |
| $exercise->tutors()->sync($tutorsIds); | |
| } | |
| private function appendPreparers(Exercise $exercise, Request $request) | |
| { | |
| $preparersIds = $request->preparers; | |
| $exercise->preparers()->sync($preparersIds); | |
| } | |
| private function appendChild(Exercise $exercise, Request $request) | |
| { | |
| $child = ChildHelper::createNewChild($request->child); | |
| $exercise->child()->associate($child); | |
| $exercise->save(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment