Пример #1
0
 /**
  * @param \HorseStories\Models\Horses\Horse $horse
  * @param array $values
  */
 public function update(Horse $horse, array $values = [])
 {
     $horse->name = $values['name'];
     $horse->gender = $values['gender'];
     $horse->breed = $values['breed'];
     $horse->height = $values['height'];
     $horse->color = $values['color'];
     $horse->date_of_birth = DateTime::createFromFormat('d/m/Y', $values['date_of_birth']);
     $horse->life_number = $values['life_number'];
     $initialDisciplines = [];
     foreach ($horse->disciplines as $initialDiscipline) {
         $initialDisciplines[$initialDiscipline->id] = $initialDiscipline->discipline;
     }
     foreach ($values['disciplines'] as $discipline) {
         $horse->disciplines()->updateOrCreate(['discipline' => $discipline, 'horse_id' => $horse->id]);
     }
     $unwantedDisciplines = array_diff($initialDisciplines, $values['disciplines']);
     foreach ($unwantedDisciplines as $key => $values) {
         $this->disciplines->removeById($key);
     }
     $horse->save();
 }
Пример #2
0
 /**
  * @param array $values
  * @param bool $pedigree
  * @return \HorseStories\Models\Horses\Horse
  */
 public function create($values = [], $pedigree = false)
 {
     $horse = new Horse();
     $horse->name = $values['name'];
     if (!$pedigree) {
         $horse->user_id = $this->auth->user()->id;
     }
     $horse->gender = $values['gender'];
     $horse->breed = $values['breed'];
     $horse->life_number = $values['life_number'];
     $horse->color = $values['color'];
     $horse->date_of_birth = DateTime::createFromFormat('d/m/Y', $values['date_of_birth']);
     $horse->height = $values['height'];
     $horse->slug = $this->slugCreator->createForHorse($values['name']);
     $horse->save();
     if (array_key_exists('disciplines', $values)) {
         foreach ($values['disciplines'] as $discipline) {
             $horse->disciplines()->updateOrCreate(['discipline' => $discipline, 'horse_id' => $horse->id]);
         }
     }
     return $horse;
 }