has('with')) { $relations = explode(',', $request->with); $query->with($relations); } if ($request->has('q') && $request->q) { $query->where(function($q) use ($request) { $q->where('name', 'like', "%{$request->q}%") ->orWhere('email', 'like', "%{$request->q}%") ->orWhere('rfc', 'like', "%{$request->q}%"); }); } return ApiResponse::OK->response([ 'clients' => $query->paginate(config('app.pagination')), ]); } public function show(Client $client) { return ApiResponse::OK->response([ 'client' => $client ]); } public function store(Request $request) { $request->validate([ 'name' => 'nullable|string|max:255', 'email' => 'nullable|email|max:255', 'phone' => 'nullable|string|max:20', 'address' => 'nullable|string|max:500', 'rfc' => 'nullable|string|max:13', ],[ 'email.unique' => 'El correo electrónico ya está en uso por otro cliente.', 'phone.unique' => 'El teléfono ya está en uso por otro cliente.', 'rfc.unique' => 'El RFC ya está en uso por otro cliente.', ]); try{ $client = Client::create($request->only([ 'name', 'email', 'phone', 'address', 'rfc', ])); return ApiResponse::OK->response([ 'client' => $client, 'message' => 'Cliente creado correctamente.' ]); }catch(\Exception $e){ return ApiResponse::BAD_REQUEST->response([ 'message' => 'Error al crear el cliente.' ]); } } public function update(Request $request, Client $client) { $request->validate([ 'name' => 'nullable|string|max:255', 'email' => 'nullable|email|max:255', 'phone' => 'nullable|string|max:20', 'address' => 'nullable|string|max:500', 'rfc' => 'nullable|string|max:13', ],[ 'email.unique' => 'El correo electrónico ya está en uso por otro cliente.', 'phone.unique' => 'El teléfono ya está en uso por otro cliente.', 'rfc.unique' => 'El RFC ya está en uso por otro cliente.', ]); try{ $client->update($request->only([ 'name', 'email', 'phone', 'address', 'rfc', ])); return ApiResponse::OK->response([ 'client' => $client, 'message' => 'Cliente actualizado correctamente.' ]); }catch(\Exception $e){ return ApiResponse::BAD_REQUEST->response([ 'message' => 'Error al actualizar el cliente.' ]); } } public function destroy(Client $client) { try{ $client->delete(); return ApiResponse::OK->response([ 'message' => 'Cliente eliminado correctamente.' ]); }catch(\Exception $e){ return ApiResponse::BAD_REQUEST->response([ 'message' => 'Error al eliminar el cliente.' ]); } } }