authorize('viewAny', Service::class); $locale = app()->getLocale(); $services = Service::query() ->with(self::WITH) ->when($request->has('is_published'), fn ($q) => $q->where('is_published', $request->boolean('is_published'))) ->when($request->filled('search'), fn ($q) => $q->where("title->{$locale}", 'like', '%'.$request->string('search').'%')) ->when($request->boolean('trashed'), fn ($q) => $q->onlyTrashed()) ->orderBy('position') ->paginate(perPage: min($request->integer('per_page', 25), 100)) ->withQueryString(); return ServiceResource::collection($services); } public function store(ServiceRequest $request): JsonResponse { $this->authorize('create', Service::class); return ServiceResource::make($this->save(new Service, $request))->response()->setStatusCode(201); } public function show(Service $service): ServiceResource { $this->authorize('view', $service); return ServiceResource::make($service->load(self::WITH)); } public function update(ServiceRequest $request, Service $service): ServiceResource { $this->authorize('update', $service); return ServiceResource::make($this->save($service, $request)); } public function destroy(Service $service): JsonResponse { $this->authorize('delete', $service); $service->delete(); return response()->json(status: 204); } public function restore(Service $service): ServiceResource { $this->authorize('restore', $service); $service->restore(); return ServiceResource::make($service->load(self::WITH)); } protected function save(Service $service, ServiceRequest $request): Service { $data = $request->validated(); DB::transaction(function () use ($service, $data): void { $service->fill(TranslatablePayload::merge( $service, collect($data)->except(['technology_ids', 'project_ids'])->all(), $service->translatable, )); $service->save(); if (array_key_exists('technology_ids', $data)) { $service->technologies()->sync($data['technology_ids']); } if (array_key_exists('project_ids', $data)) { $service->projects()->sync($data['project_ids']); } }); return $service->load(self::WITH); } }