query('search')) { $q->where(function ($qq) use ($s) { $qq->where('name', 'like', "%{$s}%") ->orWhere('phone', 'like', "%{$s}%") ->orWhere('email', 'like', "%{$s}%"); }); } return response()->json($q->orderBy('name')->paginate(min((int) $request->query('per_page', 25), 100))); } public function show(Client $client): JsonResponse { return response()->json($client->load('vehicles')); } public function store(Request $request): JsonResponse { $data = $request->validate([ 'name' => 'required|string|max:120', 'phone' => 'required|string|max:40', 'phone_alt' => 'nullable|string|max:40', 'email' => 'nullable|email|max:120', 'company_name' => 'nullable|string|max:160', 'type' => 'in:individual,company', 'status' => 'in:new,active,vip,debtor,blocked,lost', 'discount_pct' => 'nullable|numeric|min:0|max:100', 'notes' => 'nullable|string', ]); $client = Client::create($data + ['type' => 'individual', 'status' => 'active']); return response()->json($client, 201); } public function update(Request $request, Client $client): JsonResponse { $data = $request->validate([ 'name' => 'sometimes|string|max:120', 'phone' => 'sometimes|string|max:40', 'phone_alt' => 'nullable|string|max:40', 'email' => 'nullable|email|max:120', 'company_name' => 'nullable|string|max:160', 'status' => 'in:new,active,vip,debtor,blocked,lost', 'discount_pct' => 'nullable|numeric|min:0|max:100', 'notes' => 'nullable|string', ]); $client->update($data); return response()->json($client); } public function destroy(Client $client): JsonResponse { $client->delete(); return response()->json(['ok' => true]); } }