all(), [ 'name' => 'required|string|max:255', 'email' => 'required|string|email|max:255|unique:users', 'password' => 'required|string|min:6|confirmed', 'type' => 'required|string|max:255|in:super_admin,manger_authority_person, decision_maker,supervisor,employee', ]); if($validator->fails()){ $error = $validator->errors(); return response()->json(compact('error'),400); } $user = User::create([ 'name' => $request['name'], 'email' => $request['email'], 'type' => $request['type'], 'password' => Hash::make($request['password']), ]); $token = JWTAuth::fromUser($user); return response()->json(compact('user','token'),201); } public function get_user_by_id ($id) { $user = User::find($id); return response()->json(compact('user')); } public function edit_current_user (Request $request) { $validator = Validator::make($request->all(), [ 'id' => 'required', 'name' => 'required|string|max:255', 'email' => 'required|string|max:255|unique:users,email,'.$request['id'], 'password' => 'required|string|min:6', 'type' => 'required|string|max:255|in:super_admin,manger_authority_person, decision_maker,supervisor,employee', ]); if($validator->fails()){ $error = $validator->errors(); return response()->json(compact('error'),400); } $user = User::find($request['id'])->update([ 'name' => $request['name'], 'email' => $request['email'], 'type' => 'super_admin', 'password' => Hash::make($request['password']), ]); $user = User::find($request['id']); return response()->json(compact('user')); } public function delete_current_users (Request $request) { $validator = Validator::make($request->all(), [ "users_id.*" => "required|integer" ]); if($validator->fails()){ $error = $validator->errors(); return response()->json(compact('error'),400); } foreach ($request['users_id'] as $key => $id) { $user = User::find($id)->delete(); } return response()->json(['status' => 'done'], 204); } public function users_list ($current_page, $per_page, $type ,$key = null) { $current_page = $current_page - 1 ; $users = DB::table('users'); if ($type!='all') { $users->where('type',$type); } if (isset($key) && $key != null) { $users->where(function ($query) use ($key){ $query->where('name','like',"%$key%") ->orWhere('email','like',"%$key%"); // ->orWhere('phone','like',"%$key%"); }); } $users = $users->whereNull('deleted_at'); $count = $users->count(); $users = $users->skip($per_page*$current_page)->limit($per_page)->orderBy('id','DESC')->get(); return response()->json(compact('users','count','per_page')); } } // class