authorize('viewAny', Client::class); $clients = Client::query() ->with(self::WITH) ->withCount('projects') ->when($request->has('is_public'), fn ($q) => $q->where('is_public', $request->boolean('is_public'))) ->when($request->filled('search'), fn ($q) => $q->where('name', 'like', '%'.$request->string('search').'%')) ->when($request->boolean('trashed'), fn ($q) => $q->onlyTrashed()) ->orderBy('position') ->orderBy('name') ->paginate(perPage: min($request->integer('per_page', 25), 100)) ->withQueryString(); return ClientResource::collection($clients); } public function store(ClientRequest $request): JsonResponse { $this->authorize('create', Client::class); return ClientResource::make($this->save(new Client, $request))->response()->setStatusCode(201); } public function show(Client $client): ClientResource { $this->authorize('view', $client); return ClientResource::make($client->load(self::WITH)->loadCount('projects')); } public function update(ClientRequest $request, Client $client): ClientResource { $this->authorize('update', $client); return ClientResource::make($this->save($client, $request)); } public function destroy(Client $client): JsonResponse { $this->authorize('delete', $client); $client->delete(); return response()->json(status: 204); } public function restore(Client $client): ClientResource { $this->authorize('restore', $client); $client->restore(); return ClientResource::make($client->load(self::WITH)->loadCount('projects')); } protected function save(Client $client, ClientRequest $request): Client { $client->fill(TranslatablePayload::merge($client, $request->validated(), $client->translatable)); $client->save(); return $client->load(self::WITH)->loadCount('projects'); } }