source: app/Http/Controllers/Auth/RegisterController.php@ dfae77e

Last change on this file since dfae77e was dfae77e, checked in by Igor Danilovski <igor_danilovski@…>, 2 years ago
  • Initial commit;
  • Property mode set to 100644
File size: 3.4 KB
Line 
1<?php
2
3namespace App\Http\Controllers\Auth;
4
5use App\Http\Controllers\Controller;
6use App\Http\Controllers\Utils\UtilController;
7use App\Models\Artist;
8use App\Models\Organizer;
9use App\Providers\RouteServiceProvider;
10use App\Models\User;
11use Illuminate\Foundation\Auth\RegistersUsers;
12use Illuminate\Http\Request;
13use Illuminate\Support\Facades\Hash;
14use Illuminate\Support\Facades\Log;
15use Illuminate\Support\Facades\Validator;
16use Laravolt\Avatar\Avatar;
17use Exception;
18
19class RegisterController extends Controller
20{
21 /*
22 |--------------------------------------------------------------------------
23 | Register Controller
24 |--------------------------------------------------------------------------
25 |
26 | This controller handles the registration of new users as well as their
27 | validation and creation. By default this controller uses a trait to
28 | provide this functionality without requiring any additional code.
29 |
30 */
31
32 use RegistersUsers;
33
34 /**
35 * Where to redirect users after registration.
36 *
37 * @var string
38 */
39 protected $redirectTo = RouteServiceProvider::HOME;
40
41 /**
42 * Create a new controller instance.
43 *
44 * @return void
45 */
46 public function __construct()
47 {
48 $this->middleware('guest');
49 }
50
51 /**
52 * Get a validator for an incoming registration request.
53 *
54 * @param array $data
55 * @return \Illuminate\Contracts\Validation\Validator
56 */
57 protected function validator(array $data)
58 {
59 return Validator::make($data, [
60 'name' => ['required', 'string', 'max:80'],
61 'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
62 'password' => ['required', 'string', 'min:8', 'confirmed'],
63 'account_type' => ['required', 'string', 'regex:(artist|organizer)'],
64 ]);
65 }
66
67 /**
68 * Create a new user instance after a valid registration.
69 *
70 * @param array $data
71 * @return User|void
72 */
73 protected function create(array $data)
74 {
75 $username = strtolower(preg_replace('/\s+/', '', $data['name'])) . uniqid();
76 $image_name = ($username . '-' . uniqid() . '.jpg');
77
78 $user = User::create([
79 'name' => $data['name'],
80 'email' => $data['email'],
81 'password' => Hash::make($data['password']),
82 'username' => $username,
83 'profile_picture' => $image_name,
84 'type' => strtoupper($data['account_type']),
85 ]);
86
87 switch(strtolower($data['account_type'])) {
88 case 'artist':
89 $artist = Artist::create([
90 'user_id' => $user->id
91 ]);
92
93 $artist->user()->associate($user);
94
95 try {
96 $artist->save();
97 } catch (Exception $e) {
98 Log::error($e->getMessage());
99 }
100 break;
101 case 'organizer':
102 $organizer = Organizer::create([
103 'user_id' => $user->id
104 ]);
105
106 $organizer->user()->associate($user);
107
108 try {
109 $organizer->save();
110 } catch (Exception $e) {
111 Log::error($e->getMessage());
112 }
113 }
114
115 UtilController::saveAvatarImage($image_name, $data['name']);
116
117 return $user;
118 }
119}
Note: See TracBrowser for help on using the repository browser.