/** * @param Collection|BaseModel $modelOrCollection * @param Request $request * @return mixed */ protected function getWithNested($modelOrCollection, Request $request) { if (!$modelOrCollection instanceof BaseModel && !$modelOrCollection instanceof Collection) { throw new \InvalidArgumentException('Model must be instance of Model or Collection'); } $nested = $request->headers->get('With-Nested'); if (!$nested) { return $modelOrCollection; } $requestedRelations = explode(', ', $nested); try { $modelOrCollection->load($requestedRelations); } catch (\BadMethodCallException $e) { throw new BadRequestException(sprintf('Invalid `With-Nested` request - one or more of the following relationships do not exist for %s:[%s]', get_class($modelOrCollection), $nested), null, $e); } return $modelOrCollection; }
/** * Set a given attribute on the model. * * @param string $key * @param mixed $value * @return void */ public function setAttribute($key, $value) { if (in_array($key, $this->getDates()) && $value) { if (!$value instanceof Carbon && !$value instanceof DateTime) { $value = new Carbon($value); $this->attributes[$key] = $value; return; } } parent::setAttribute($key, $value); }
/** * @param Request $request * @param $id * @param BaseModel $model * @param bool|true $requireEntityKey * @return bool */ protected function checkEntityIdMatchesRoute(Request $request, $id, BaseModel $model, $requireEntityKey = true) { $keyName = $model->getKeyName(); if (!$request->has($keyName)) { if (!$requireEntityKey) { return true; //it is ok if the key is not set (for patch requests etc) } else { throw new BadRequestException("Request entity must include entity id ({$keyName}) for " . get_class($model)); } } if ((string) $request->input($keyName) !== (string) $id) { throw new BadRequestException("Provided entity body does not match route parameter. The entity key cannot be updated"); } return true; }
/** * @param BaseModel $model * @return mixed|string */ protected function getItemKey(BaseModel $model) { if ($model->exists) { return $model->getKey(); } return $this->getItemHash($model); }