From d2c2a6e0aeb34ee83c77828bbb3f8d778e988ba0 Mon Sep 17 00:00:00 2001 From: "M. Iqbal Effendi" Date: Sun, 2 Jun 2024 22:11:48 +0800 Subject: [PATCH] image feature --- app/Http/Controllers/ProfileController.php | 21 ++++- app/Http/Controllers/TeacherController.php | 22 ++++- app/Http/Requests/ProfileUpdateRequest.php | 10 +++ app/Models/User.php | 19 +++- lang/en/button.php | 1 + lang/en/label.php | 1 + lang/id/button.php | 1 + lang/id/label.php | 1 + resources/views/layouts/dashboard.blade.php | 1 + .../pages/account/change-password.blade.php | 10 ++- .../views/pages/account/profile.blade.php | 86 +++++++++++++++---- .../views/pages/teacher/create.blade.php | 82 +++++++++++++++--- resources/views/pages/teacher/edit.blade.php | 80 ++++++++++++++--- resources/views/pages/teacher/index.blade.php | 9 +- 14 files changed, 295 insertions(+), 49 deletions(-) diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 860dda6..f385c54 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -7,7 +7,9 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Redirect; +use Illuminate\Support\Facades\Storage; use Illuminate\View\View; +use Random\RandomException; class ProfileController extends Controller { @@ -23,10 +25,27 @@ public function edit(Request $request): View /** * Update the user's profile information. + * + * @throws RandomException */ public function update(ProfileUpdateRequest $request): RedirectResponse { - $request->user()->fill($request->validated()); + $data = $request->validated(); + + if (isset($data['image']) || $data['image'] === null) { + unset($data['image']); + } + + if ($request->hasFile('image')) { + if ($request->user()->image) { + Storage::delete("public/{$request->user()->image}"); + } + + $data['image'] = time().random_int(0, PHP_INT_MAX).'.'.$request->file('image')->extension(); + Storage::putFileAs('public', $request->file('image'), $data['image']); + } + + $request->user()->fill($data); if ($request->user()->isDirty('email')) { $request->user()->email_verified_at = null; diff --git a/app/Http/Controllers/TeacherController.php b/app/Http/Controllers/TeacherController.php index 1d3047a..22a35d8 100644 --- a/app/Http/Controllers/TeacherController.php +++ b/app/Http/Controllers/TeacherController.php @@ -10,6 +10,7 @@ use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Facades\Storage; use Illuminate\View\View; use Yajra\DataTables\DataTables; @@ -39,8 +40,9 @@ class="btn p-0 dropdown-toggle hide-arrow" '; }) + ->editColumn('image', fn ($row) => 'user-avatar') ->editColumn('gender', fn ($row) => __('label.'.$row->gender)) - ->rawColumns(['action']) + ->rawColumns(['action', 'image']) ->make(); } @@ -65,6 +67,11 @@ public function store(StoreTeacherRequest $request): RedirectResponse $data['role'] = Role::TEACHER; $data['password'] = bcrypt('password'); + if ($request->hasFile('image')) { + $data['image'] = time().random_int(0, PHP_INT_MAX).'.'.$request->file('image')->extension(); + Storage::putFileAs('public', $request->file('image'), $data['image']); + } + User::create($data); return redirect()->route('teacher.index')->with('notification', $this->successNotification('notification.success_create', 'menu.teacher')); @@ -101,7 +108,18 @@ public function edit(User $teacher): View public function update(UpdateTeacherRequest $request, User $teacher): RedirectResponse { try { - $teacher->update($request->validated()); + $data = $request->validated(); + + if ($request->hasFile('image')) { + if ($teacher->image) { + Storage::delete("public/{$teacher->image}"); + } + + $data['image'] = time().random_int(0, PHP_INT_MAX).'.'.$request->file('image')->extension(); + Storage::putFileAs('public', $request->file('image'), $data['image']); + } + + $teacher->update($data); return back()->with('notification', $this->successNotification('notification.success_update', 'menu.teacher')); } catch (\Throwable $throwable) { diff --git a/app/Http/Requests/ProfileUpdateRequest.php b/app/Http/Requests/ProfileUpdateRequest.php index 7f6e549..78c6669 100644 --- a/app/Http/Requests/ProfileUpdateRequest.php +++ b/app/Http/Requests/ProfileUpdateRequest.php @@ -18,6 +18,11 @@ public function rules(): array return [ 'name' => ['required', 'string', 'max:255'], 'email' => ['required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($this->user()->id)], + 'image' => ['nullable', 'image', 'max:2048'], + 'phone' => ['required'], + 'address' => ['nullable'], + 'marital_status' => ['nullable'], + 'gender' => ['required'], ]; } @@ -26,6 +31,11 @@ public function attributes(): array return [ 'name' => __('field.name'), 'email' => __('field.email'), + 'image' => __('field.image'), + 'phone' => __('field.phone'), + 'address' => __('field.address'), + 'marital_status' => __('field.marital_status'), + 'gender' => __('field.gender'), ]; } } diff --git a/app/Models/User.php b/app/Models/User.php index c412b83..7dd2388 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -8,6 +8,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; +use Illuminate\Support\Facades\Storage; class User extends Authenticatable { @@ -45,6 +46,16 @@ class User extends Authenticatable 'image_url', ]; + protected static function boot(): void + { + parent::boot(); + static::deleting(function (User $user) { + if ($user->image) { + Storage::delete("public/$user->image"); + } + }); + } + /** * Get the attributes that should be cast. * @@ -61,7 +72,13 @@ protected function casts(): array public function imageUrl(): Attribute { return new Attribute( - get: fn () => 'https://ui-avatars.com/api/?name='.$this->name, + get: function () { + if ($this->image) { + return asset('storage/'.$this->image); + } + + return asset('404_Black.jpg'); + } ); } diff --git a/lang/en/button.php b/lang/en/button.php index c1e4ba9..4fc921f 100644 --- a/lang/en/button.php +++ b/lang/en/button.php @@ -10,5 +10,6 @@ 'login' => 'Sign In', 'register' => 'Sign Up', 'delete_permanently' => 'Delete My Account', + 'upload' => 'Upload', ]; diff --git a/lang/en/label.php b/lang/en/label.php index 448ec1f..6f90594 100644 --- a/lang/en/label.php +++ b/lang/en/label.php @@ -41,4 +41,5 @@ 'male' => 'Male', 'female' => 'Female', 'required' => 'Required', + 'allowed_image_upload' => 'Allowed JPG or PNG. Max size of 2MB', ]; diff --git a/lang/id/button.php b/lang/id/button.php index 5812aee..08b8893 100644 --- a/lang/id/button.php +++ b/lang/id/button.php @@ -10,5 +10,6 @@ 'login' => 'Masuk', 'register' => 'Buat Akun', 'delete_permanently' => 'Hapus Akun Permanen', + 'upload' => 'Unggah', ]; diff --git a/lang/id/label.php b/lang/id/label.php index d1f81e4..8622315 100644 --- a/lang/id/label.php +++ b/lang/id/label.php @@ -41,4 +41,5 @@ 'male' => 'Laki-laki', 'female' => 'Perempuan', 'required' => 'Wajib', + 'allowed_image_upload' => 'Hanya boleh JPG atau PNG. Ukuran maksimal 2MB', ]; diff --git a/resources/views/layouts/dashboard.blade.php b/resources/views/layouts/dashboard.blade.php index a959cfb..4e0467e 100644 --- a/resources/views/layouts/dashboard.blade.php +++ b/resources/views/layouts/dashboard.blade.php @@ -47,6 +47,7 @@ @push('script') + @endpush diff --git a/resources/views/pages/teacher/create.blade.php b/resources/views/pages/teacher/create.blade.php index 165207f..993eed8 100644 --- a/resources/views/pages/teacher/create.blade.php +++ b/resources/views/pages/teacher/create.blade.php @@ -14,31 +14,87 @@
-
-
- @csrf + + @csrf +
+
+ + user-avatar + +
+ + + + {{ __('label.allowed_image_upload') }} +
+
+
+
+
- +
- +
- +
- +
- +
- +
- - - -
+
+ + +
+
+
@endsection + +@push('script') + +@endpush diff --git a/resources/views/pages/teacher/edit.blade.php b/resources/views/pages/teacher/edit.blade.php index 09afd6a..e709fd7 100644 --- a/resources/views/pages/teacher/edit.blade.php +++ b/resources/views/pages/teacher/edit.blade.php @@ -14,33 +14,89 @@
-
-
+ +
+
+ + user-avatar + +
+ + + + {{ __('label.allowed_image_upload') }} +
+
+
+
+
@csrf @method('PUT')
- +
- +
- +
- +
- +
- +
- - - -
+
+ + +
+
+
@endsection + +@push('script') + +@endpush diff --git a/resources/views/pages/teacher/index.blade.php b/resources/views/pages/teacher/index.blade.php index 3e4c0ca..802269c 100644 --- a/resources/views/pages/teacher/index.blade.php +++ b/resources/views/pages/teacher/index.blade.php @@ -15,6 +15,7 @@ + @@ -38,21 +39,27 @@ @endpush
{{ __('field.name') }} {{ __('field.gender') }} {{ __('field.email') }}