Skip to content

Commit

Permalink
added avatar to user
Browse files Browse the repository at this point in the history
  • Loading branch information
A1Gard committed Sep 24, 2024
1 parent 388a22f commit a45724c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
21 changes: 21 additions & 0 deletions app/Http/Controllers/Admin/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\User;
use Illuminate\Http\Request;
use App\Helper;
use Spatie\Image\Image;
use function App\Helpers\hasCreateRoute;

class UserController extends XController
Expand All @@ -34,6 +35,7 @@ class UserController extends XController
public function save($user, $request)
{

// dd($request->all());
if ($user->role == 'DEVELOPER' && !auth()->user()->hasRole('developer')) {
abort(403);
}
Expand Down Expand Up @@ -70,6 +72,25 @@ public function save($user, $request)
}

}

if ($request->hasFile('avatar')) {
$name = time() . '.' . request()->avatar->getClientOriginalExtension();
$user->avatar = $name;
$request->file('avatar')->storeAs('public/users', $name);
$format = $request->file('avatar')->guessExtension();
$format = 'webp';
$key = 'avatar';

$i = Image::load($request->file($key)->getPathname())
->optimize()
->width(500)
->height(500)
->crop(500, 500)
// ->nonQueued()
->format($format);
$i->save(storage_path() . '/app/public/users/'. $user->avatar);
$user->save();
}
return $user;

}
Expand Down
10 changes: 10 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,14 @@ public function evaluations(){
->where('evaluationable_id',$this->id);
})->get();
}


public function avatar(){
if ($this->avatar == null || trim($this->avatar) == ''){
return asset('assets/default/unknown.svg');
}


return \Storage::url('users/' . $this->avatar);
}
}
9 changes: 8 additions & 1 deletion resources/views/admin/users/user-form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@
@endif
</ul>
</div>

<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-user-3-line"></i>
{{__("Avatar")}}
</h3>
<img src="{{$item->avatar()}}" class="img-fluid mb-3" alt="" data-open-file="#avatar-input">
<input type="file" name="avatar" id="avatar-input" accept="image/jpeg">
</div>
</div>
<div class="col-lg-9 ps-xl-1 ps-xxl-1">
<div class="general-form ">
Expand Down
2 changes: 1 addition & 1 deletion resources/views/home.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="card-body">
<div class="row">
<div class="col-3 text-center">
<img src="{{asset('panel/images/xshop-logo.svg')}}" class="avatar-x64" alt="">
<img src="{{auth()->user()->avatar()}}" class="avatar-x64" alt="">
</div>
<div class="col-9 pt-1">
{{__("Welcome back")}}
Expand Down

0 comments on commit a45724c

Please sign in to comment.