Skip to content

Commit

Permalink
Profile picture
Browse files Browse the repository at this point in the history
  • Loading branch information
Saodus committed Nov 23, 2024
1 parent 384c040 commit fac66c4
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 37 deletions.
38 changes: 29 additions & 9 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Illuminate\Support\Facades\Redirect;
use Illuminate\View\View;

use Illuminate\Support\Facades\Storage;

class ProfileController extends Controller
{
/**
Expand All @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Requests/ProfileUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
];
}
}
1 change: 1 addition & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class User extends Authenticatable
protected $fillable = [
'name',
'email',
'image',
'password',
];

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->string('image')->nullable();

});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
//
$table->dropColumn('image');
});
}
};
Binary file added public/source/assets/avatar/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
}
4 changes: 2 additions & 2 deletions resources/js/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import './bootstrap';
import "./bootstrap";

import Alpine from 'alpinejs';
import Alpine from "alpinejs";

window.Alpine = Alpine;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
@csrf
</form>

<form method="post" action="{{ route('profile.update') }}" class="mt-6 space-y-6">
<form method="post" action="{{ route('profile.update') }}" enctype="multipart/form-data" class="mt-6 space-y-6">
@csrf
@method('patch')

Expand All @@ -27,38 +27,39 @@
<x-input-label for="email" :value="__('Email')" />
<x-text-input id="email" name="email" type="email" class="mt-1 block w-full" :value="old('email', $user->email)" required autocomplete="username" />
<x-input-error class="mt-2" :messages="$errors->get('email')" />
</div>

@if ($user instanceof \Illuminate\Contracts\Auth\MustVerifyEmail && ! $user->hasVerifiedEmail())
<div>
<p class="text-sm mt-2 text-gray-800 dark:text-gray-200">
{{ __('Your email address is unverified.') }}

<button form="send-verification" class="underline text-sm text-gray-600 dark:text-gray-400 hover:text-gray-900 dark:hover:text-gray-100 rounded-md focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 dark:focus:ring-offset-gray-800">
{{ __('Click here to re-send the verification email.') }}
</button>
</p>

@if (session('status') === 'verification-link-sent')
<p class="mt-2 font-medium text-sm text-green-600 dark:text-green-400">
{{ __('A new verification link has been sent to your email address.') }}
</p>
@endif
</div>
@endif
<div>
<label for="image-upload">
<img src="{{ $user->image ? asset('storage/' . $user->image) : asset('/source/assets/avatar/avatar.png') }}"
alt="Image" class="avatar" style="cursor: pointer;">
</label>
<input type="file" id="image-upload" name="image" accept="image/*" style="display: none;" onchange="previewImage(event)">
</div>

<div class="flex items-center gap-4">
<x-primary-button>{{ __('Save') }}</x-primary-button>

@if (session('status') === 'profile-updated')
<p
x-data="{ show: true }"
x-show="show"
x-transition
x-init="setTimeout(() => show = false, 2000)"
class="text-sm text-gray-600 dark:text-gray-400"
>{{ __('Saved.') }}</p>
<p
x-data="{ show: true }"
x-show="show"
x-transition
x-init="setTimeout(() => show = false, 2000)"
class="text-sm text-gray-600 dark:text-gray-400">{{ __('Saved.') }}</p>
@endif
</div>
</form>
</section>
<script>
function previewImage(event) {
const reader = new FileReader();
const avatar = document.querySelector('.avatar');
reader.onload = function() {
avatar.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
}
</script>
</section>

0 comments on commit fac66c4

Please sign in to comment.