Untitled

mail@pastecode.io avatar
unknown
php
2 years ago
1.5 kB
1
Indexable
Never
$messages = array(
              'name.required' => 'Name cannot be left blank.',
              'name.max' => 'name and surname can be up to 255 characters.',
              'email.required' => 'E-mail cannot be left blank.',
              'email.unique' => 'Email already use.',
              'password.required' => 'Password cannot be left blank.',
              'phone.required' => 'Phone number cannot be left blank.',
          );

          $validator = Validator::make($request->all(),
          [
              "name"=>"required|max:255",
              "email"=>"required|unique:users",
              "password"=>"required",
              "phone" => "required"
          ],$messages);

          if ($validator->fails()) {
              $fails=array_values($validator->getMessageBag()->toArray())[0];
              $error = $fails[0];
              Format::$message = $error;
              return Format::withoutData();
          }
          $bcrypt = new Bcrypt();
          $password = $bcrypt->hash_password($request->password);
          $user = User::create([
              'first_name' => $request->name,
              'email' => $request->email,
              'password' => $password,
              'phone_number' => $request->phone,
          ]);
          $token = $user->createToken('auth_token')->plainTextToken;
          Format::$message = 'successfully registered';
          Format::$data = $token;
          return Format::withoutData();