/** * */ public function update(Request $request, $id) { $entry = Entry::find($id); $data = array_filter(array_diff_assoc($request->except(['restoration_type_id', 'folders']), $entry->toArray()), 'removeFalseKeepZero'); $entry->update($data); $restorationType = RestorationType::find($request->get('restoration_type_id')); $entry->restorationType()->associate($restorationType); $entry->save(); $entry->folders()->sync($request->get('folder_ids')); return $this->responseOk($entry); }
public function run() { $this->faker = Faker::create(); Entry::truncate(); DB::table('entry_folder')->truncate(); foreach (range(0, 20) as $index) { $originalRestorationDate = $this->faker->dateTimeBetween('-10 years', '-2 years')->format('Y-m-d'); $lastPhotoDate = $this->faker->dateTimeBetween('-2 years', 'now')->format('Y-m-d'); $restorationAge = Carbon::createFromFormat('Y-m-d', $lastPhotoDate)->diffInMonths(Carbon::createFromFormat('Y-m-d', $originalRestorationDate)) / 12; $restorationAge = round($restorationAge * 2) / 2; $entry = new Entry(['first_name' => $this->faker->firstName, 'last_name' => $this->faker->lastName, 'tooth_number' => $this->faker->numberBetween(1, 6), 'original_restoration_date' => $originalRestorationDate, 'last_photo_date' => $lastPhotoDate, 'restoration_age' => $restorationAge, 'note' => $this->faker->sentence()]); $restorationTypeIds = RestorationType::lists('id')->all(); $restorationTypeId = $this->faker->randomElement($restorationTypeIds); $restorationType = RestorationType::find($restorationTypeId); $entry->restorationType()->associate($restorationType); $entry->save(); $folderCount = $this->faker->numberBetween(1, 2); $folder_ids = $this->faker->randomElements(Folder::lists('id')->all(), $folderCount); foreach ($folder_ids as $folder_id) { $entry->folders()->attach($folder_id); } } }