safriansah

ravel-auth-controller-register

Oct 23rd, 2020
1,048
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public function register (Request $request) {
  2.     DB::beginTransaction();
  3.     try {
  4.         //code...
  5.         $validator = Validator::make($request->all(), [
  6.             'username' => 'required|max:255|unique:users',
  7.             'password' => 'required|max:255',
  8.             'fullname' => 'required|max:255',
  9.             'email' => 'required|email|unique:profile',
  10.             'phone' => 'required|numeric|unique:profile'
  11.         ]);
  12.  
  13.         if ($validator->fails()) {
  14.             return $this->getResponse(406, $validator->errors()->first());
  15.         }
  16.  
  17.         $user = new Users;
  18.         $user->username = $request->username;
  19.         $user->password = Hash::make($request->password);
  20.         $user->status = 1;
  21.         $user->push();
  22.  
  23.         $profile = new Profile;
  24.         $profile->id_user = $user->id;
  25.         $profile->fullname = $request->fullname;
  26.         $profile->email = $request->email;
  27.         $profile->phone = $request->phone;
  28.         $profile->push();
  29.  
  30.         DB::commit();
  31.         return $this->getResponse(200);
  32.     } catch (\Throwable $th) {
  33.         DB::rollback();
  34.         //throw $th;
  35.         return $this->getResponse(500);
  36.     }
  37. }
RAW Paste Data