diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index a48eb8d..8bae238 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -9,6 +9,8 @@ use Illuminate\Support\Facades\Redirect; use Illuminate\View\View; +use Illuminate\Support\Facades\Storage; + class ProfileController extends Controller { /** @@ -24,18 +26,36 @@ public function edit(Request $request): View /** * Update the user's profile information. */ - public function update(ProfileUpdateRequest $request): RedirectResponse - { - $request->user()->fill($request->validated()); - if ($request->user()->isDirty('email')) { - $request->user()->email_verified_at = null; - } + public function update(ProfileUpdateRequest $request): RedirectResponse + { + $validated = $request->validated(); + + if ($request->hasFile('image')) { + $user = $request->user(); - $request->user()->save(); + if ($user->image) { + Storage::disk('public')->delete($user->image); + } + + $path = $request->file('image')->store('avatar', 'public'); + $validated['image'] = $path; + } else { + unset($validated['image']); + } + + $request->user()->fill($validated); + + if ($request->user()->isDirty('email')) { + $request->user()->email_verified_at = null; + } + + $request->user()->save(); + + return Redirect::route('profile.edit')->with('status', 'profile-updated'); + } + - return Redirect::route('profile.edit')->with('status', 'profile-updated'); - } /** * Delete the user's account. diff --git a/app/Http/Requests/ProfileUpdateRequest.php b/app/Http/Requests/ProfileUpdateRequest.php index b2ef6bb..469f892 100644 --- a/app/Http/Requests/ProfileUpdateRequest.php +++ b/app/Http/Requests/ProfileUpdateRequest.php @@ -23,8 +23,10 @@ public function rules(): array 'lowercase', 'email', 'max:255', + Rule::unique(User::class)->ignore($this->user()->id), ], + 'image' => ['nullable', 'image', 'mimes:jpeg,png,jpg', 'max:2048'], ]; } } diff --git a/app/Models/User.php b/app/Models/User.php index 445fd52..27df363 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -19,6 +19,7 @@ class User extends Authenticatable protected $fillable = [ 'name', 'email', + 'image', 'password', ]; diff --git a/database/migrations/2024_11_23_144334_add_profile_picture_to_users.php b/database/migrations/2024_11_23_144334_add_profile_picture_to_users.php new file mode 100644 index 0000000..d1bed01 --- /dev/null +++ b/database/migrations/2024_11_23_144334_add_profile_picture_to_users.php @@ -0,0 +1,31 @@ +string('image')->nullable(); + + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('users', function (Blueprint $table) { + // + $table->dropColumn('image'); + }); + } +}; diff --git a/public/source/assets/avatar/avatar.png b/public/source/assets/avatar/avatar.png new file mode 100644 index 0000000..63c8cb6 Binary files /dev/null and b/public/source/assets/avatar/avatar.png differ diff --git a/resources/css/app.css b/resources/css/app.css index c2c10a4..8dcf861 100644 --- a/resources/css/app.css +++ b/resources/css/app.css @@ -40,3 +40,10 @@ .messagesBackground { background-image: url('../../public/source/assets/images/messageBackground.png'); } + +.avatar { + vertical-align: middle; + width: 50px; + height: 50px; + border-radius: 50%; + } \ No newline at end of file diff --git a/resources/js/app.js b/resources/js/app.js index a8093be..3f6ccfb 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -1,6 +1,6 @@ -import './bootstrap'; +import "./bootstrap"; -import Alpine from 'alpinejs'; +import Alpine from "alpinejs"; window.Alpine = Alpine; diff --git a/resources/views/profile/partials/update-profile-information-form.blade.php b/resources/views/profile/partials/update-profile-information-form.blade.php index 7273fff..5e54369 100644 --- a/resources/views/profile/partials/update-profile-information-form.blade.php +++ b/resources/views/profile/partials/update-profile-information-form.blade.php @@ -13,7 +13,7 @@ @csrf -
- + + \ No newline at end of file