Skip to content

Commit

Permalink
Update user UI avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
JosueOb committed Nov 13, 2021
1 parent d05316a commit 28aed40
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
31 changes: 23 additions & 8 deletions app/Http/Controllers/Profile/ProfileInformationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\Profile\UpdateProfileInformationRequest;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Illuminate\View\View;

class ProfileInformationController extends Controller
{
private string $ui_avatar_api = "https://ui-avatars.com/api/?name=*+*&size=128";

public function create(): view
{
return view('profile.show', [
Expand All @@ -24,7 +28,7 @@ public function update(UpdateProfileInformationRequest $request): RedirectRespon

$user = $request->user();

/*Update the model using the save() method*/
/*Update the model using Eloquent*/
$user->first_name = $validated['first_name'];
$user->last_name = $validated['last_name'];
$user->username = $validated['username'];
Expand All @@ -34,14 +38,25 @@ public function update(UpdateProfileInformationRequest $request): RedirectRespon
$user->address = $validated['address'];
$user->save();

/*Before saving in the DB, a casting of the date of birth string is performed*/
/*$validated['birthdate'] = Carbon::createFromFormat(
'd/m/Y',
$validated['birthdate']
)->format('Y-m-d');*/
/*Update the model using the forceFill() and save() methods*/
// $user->forceFill($validated)->save();
$this->updateUIAvatar($user);

return back()->with('status', 'Profile update successfully');
}

private function updateUIAvatar(User $user): void
{
$user_image = $user->image;
$image_path = $user_image->path;
if (Str::startsWith($image_path, 'https://')) {
$user_image->path = Str::replaceArray(
'*',
[
$user->first_name,
$user->last_name
],
$this->ui_avatar_api
);
$user_image->save();
}
}
}
4 changes: 3 additions & 1 deletion resources/views/profile/update-profile-information.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class="block mt-2 w-full"
class="block mt-2 w-full"
type="text"
name="birthdate"
maxlength="50"
maxlength="10"
:value="old('birthdate') ?? $user->birthdate"
placeholder="dd/mm/yyyy"/>

Expand All @@ -85,6 +85,7 @@ class="block mt-2 w-full"
class="block mt-2 w-full"
type="text"
name="personal_phone"
maxlength="10"
:value="old('personal_phone') ?? $user->personal_phone"
placeholder="Example: 0989999999"
required/>
Expand All @@ -100,6 +101,7 @@ class="block mt-2 w-full"
class="block mt-2 w-full"
type="text"
name="home_phone"
maxlength="9"
:value="old('home_phone') ?? $user->home_phone"
placeholder="Example: 022999999"
required/>
Expand Down

0 comments on commit 28aed40

Please sign in to comment.