authorize('viewAny', Unit::class); $filters = $request->all(); $results = Unit::query() ->applyFilters($filters) ->whereCompany() ->latest() ->paginateData($request->input('limit', self::DEFAULT_LIMIT)); return UnitResource::collection($results); } /** * A single unit. */ public function show(Unit $unit) { $this->authorize('view', $unit); return new UnitResource($unit); } /** * Register a unit under the acting company. * * The resource answers 201 by itself, because the wrapped model was just * created and the verb is POST. */ public function store(UnitRequest $request) { $this->authorize('create', Unit::class); $payload = $request->getUnitPayload(); return new UnitResource(Unit::create($payload)); } /** * Rename an existing unit. */ public function update(UnitRequest $request, Unit $unit) { $this->authorize('update', $unit); $payload = $request->getUnitPayload(); $unit->update($payload); return new UnitResource($unit); } /** * Retire a unit, provided no catalog item still measures by it. * * A unit in use is refused with the validation-shaped rejection above. * A successful removal reports its outcome as a sentence under `success` * rather than as a flag -- kept as is, clients read the text. */ public function destroy(Unit $unit) { $this->authorize('delete', $unit); if ($unit->items()->exists()) { return respondJson(self::IN_USE_ERROR, self::IN_USE_MESSAGE); } $unit->delete(); return response()->json(['success' => 'Unit deleted successfully']); } }