/** * Display a paginated list of the requested resource. * * @param \ByCedric\Allay\Contracts\Resource\Manager $manager * @param \ByCedric\Allay\Contracts\Resource\Resolver $resolver * @return mixed */ public function index(Manager $manager, Resolver $resolver) { $resource = $manager->make($resolver->getResource()); if ($resource instanceof Readable) { $resource = $resource->getReadableQuery($resource->newQuery()); } return $resource->paginate(); }
/** * Display a single resource, that matches the requested id. * * @param \ByCedric\Allay\Contracts\Resource\Manager $manager * @param \ByCedric\Allay\Contracts\Resource\Resolver $resolver * @return mixed */ public function show(Manager $manager, Resolver $resolver) { $resource = $manager->make($resolver->getResource()); if ($resource instanceof Readable) { $resource = $resource->getReadableQuery($resource->newQuery()); } return $resource->findOrFail($resolver->getId()); }
/** * Delete the requested resource entity, removing it from the database. * * @param \ByCedric\Allay\Contracts\Resource\Manager $manager * @param \ByCedric\Allay\Contracts\Resource\Resolver $resolver * @return mixed */ public function destroy(Manager $manager, Resolver $resolver) { $resource = $manager->make($resolver->getResource()); if ($resource instanceof Writable) { $resource = $resource->getWritableQuery($resource->newQuery()); } $resource = $resource->findOrFail($resolver->getId()); $resource->delete(); return $resource; }
/** * Create a new entity of the requested resource, with the given attributes. * * @param \Illuminate\Http\Request $request * @param \ByCedric\Allay\Contracts\Resource\Manager $manager * @param \ByCedric\Allay\Contracts\Resource\Resolver $resolver * @throws \ByCedric\Allay\Exceptions\ResourceMissingValidationException * @throws \Illuminate\Contracts\Validation\ValidationException * @return mixed */ public function store(Request $request, Manager $manager, Resolver $resolver) { $resource = $manager->make($resolver->getResource()); if (!$resource instanceof Validatable) { throw new ResourceMissingValidationException($resolver->getResource()); } $validator = $resource->fill($request->all())->getValidatorInstance(); if ($validator->fails()) { throw new ValidationException($validator); } $resource->save(); return $resource; }
/** * Display a paginated list of the relation of the requested resource. * * @param \ByCedric\Allay\Contracts\Resource\Manager $manager * @param \ByCedric\Allay\Contracts\Resource\Resolver $resolver * @throws \ByCedric\Allay\Exceptions\ResourceMissingRelatableException * @throws \ByCedric\Allay\Exceptions\ResourceRelationNotFoundException * @return mixed */ public function relatedIndex(Manager $manager, Resolver $resolver) { $resource = $manager->make($resolver->getResource()); if (!$resource instanceof Relatable) { throw new ResourceMissingRelatableException($resolver->getResource()); } $method = $resolver->getRelationMethod(); $allowed = $resource->getRelatableMethods(); if (!in_array($method, $allowed) || !is_callable([$resource, $method])) { throw new ResourceRelationNotFoundException($resolver->getResource(), $resolver->getRelation()); } if ($resource instanceof Readable) { $resource = $resource->getReadableQuery($resource->newQuery()); } $resource = $resource->findOrFail($resolver->getId()); $relation = $resource->{$method}(); $relatedResource = $relation->getRelated(); if ($relatedResource instanceof Readable) { $relation = $relatedResource->getReadableQuery($relation->getQuery()); } return $relation->paginate(); }
/** * Get the model name, without the namespace. * * @param \Illuminate\Database\Eloquent\ModelNotFoundException $error * @return string */ protected function getModelName(ModelNotFoundException $error) { return $this->resources->name($error->getModel()); }