function add($request, $response, $args) { $add = false; $body = $request->getParsedBody(); $body['dateStarted'] = $this->formatDate($body['dateStarted']); if ($body['dateFinished']) { $body['dateFinished'] = $this->formatDate($body['dateFinished']); } $stillUsing = false; if (isset($body['stillUsing'])) { $stillUsing = $body['stillUsing']; } $site = new Sites(); $result = $site::where('title', $body['title'])->first(); if ($result) { if ($body['title'] !== $result->title) { $add = true; } else { $error['duplicate'] = $body['title'] . " is already present"; } } else { $add = true; } if ($add) { $site->title = $body['title']; $site->url = $body['url']; $site->dateStarted = $body['dateStarted']; $site->dateFinished = $body['dateFinished']; $site->stillUsing = $stillUsing; $site->save(); foreach ($body['skills'] as $skill) { $association = [$skill]; $site->skills()->attach($association); } foreach ($body['images'] as $image) { $association = [$image]; $site->images()->attach($association); } } }