authorize('viewAny', Testimonial::class); $testimonials = Testimonial::query() ->with(self::WITH) ->when($request->has('is_public'), fn ($q) => $q->where('is_public', $request->boolean('is_public'))) ->when($request->filled('client_id'), fn ($q) => $q->where('client_id', $request->integer('client_id'))) ->when($request->filled('project_id'), fn ($q) => $q->where('project_id', $request->integer('project_id'))) ->when($request->filled('service_id'), fn ($q) => $q->where('service_id', $request->integer('service_id'))) ->when($request->filled('search'), fn ($q) => $q->where('author_name', 'like', '%'.$request->string('search').'%')) ->when($request->boolean('trashed'), fn ($q) => $q->onlyTrashed()) ->orderBy('position') ->latest('id') ->paginate(perPage: min($request->integer('per_page', 25), 100)) ->withQueryString(); return TestimonialResource::collection($testimonials); } public function store(TestimonialRequest $request): JsonResponse { $this->authorize('create', Testimonial::class); return TestimonialResource::make($this->save(new Testimonial, $request))->response()->setStatusCode(201); } public function show(Testimonial $testimonial): TestimonialResource { $this->authorize('view', $testimonial); return TestimonialResource::make($testimonial->load(self::WITH)); } public function update(TestimonialRequest $request, Testimonial $testimonial): TestimonialResource { $this->authorize('update', $testimonial); return TestimonialResource::make($this->save($testimonial, $request)); } public function destroy(Testimonial $testimonial): JsonResponse { $this->authorize('delete', $testimonial); $testimonial->delete(); return response()->json(status: 204); } public function restore(Testimonial $testimonial): TestimonialResource { $this->authorize('restore', $testimonial); $testimonial->restore(); return TestimonialResource::make($testimonial->load(self::WITH)); } protected function save(Testimonial $testimonial, TestimonialRequest $request): Testimonial { $testimonial->fill(TranslatablePayload::merge($testimonial, $request->validated(), $testimonial->translatable)); $testimonial->save(); return $testimonial->load(self::WITH); } }