diff --git a/packages/Webkul/Category/src/Models/Category.php b/packages/Webkul/Category/src/Models/Category.php index 894cc9d75..aa8c26722 100755 --- a/packages/Webkul/Category/src/Models/Category.php +++ b/packages/Webkul/Category/src/Models/Category.php @@ -50,7 +50,7 @@ class Category extends TranslatableModel implements CategoryContract */ public function image_url() { - if (!$this->image) { + if (! $this->image) { return; } diff --git a/packages/Webkul/Customer/src/Database/Migrations/2021_10_14_122221_add_image_column_to_customers_table.php b/packages/Webkul/Customer/src/Database/Migrations/2021_10_14_122221_add_image_column_to_customers_table.php new file mode 100644 index 000000000..b30e9c757 --- /dev/null +++ b/packages/Webkul/Customer/src/Database/Migrations/2021_10_14_122221_add_image_column_to_customers_table.php @@ -0,0 +1,32 @@ +string('image')->nullable()->after('email'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('customers', function (Blueprint $table) { + $table->dropColumn('image'); + }); + } +} diff --git a/packages/Webkul/Customer/src/Http/Controllers/CustomerController.php b/packages/Webkul/Customer/src/Http/Controllers/CustomerController.php index 791001beb..f86c49175 100755 --- a/packages/Webkul/Customer/src/Http/Controllers/CustomerController.php +++ b/packages/Webkul/Customer/src/Http/Controllers/CustomerController.php @@ -108,6 +108,7 @@ class CustomerController extends Controller 'password' => 'confirmed|min:6|required_with:oldpassword', 'oldpassword' => 'required_with:password', 'password_confirmation' => 'required_with:password', + 'image.*' => 'mimes:bmp,jpeg,jpg,png,webp' ]); $data = collect(request()->input())->except('_token')->toArray(); @@ -178,6 +179,8 @@ class CustomerController extends Controller } } + $this->customerRepository->uploadImages($data, $customer); + Session()->flash('success', trans('shop::app.customer.account.profile.edit-success')); return redirect()->route($this->_config['redirect']); diff --git a/packages/Webkul/Customer/src/Models/Customer.php b/packages/Webkul/Customer/src/Models/Customer.php index e06f18e66..02243dd44 100755 --- a/packages/Webkul/Customer/src/Models/Customer.php +++ b/packages/Webkul/Customer/src/Models/Customer.php @@ -16,6 +16,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Webkul\Customer\Database\Factories\CustomerFactory; use Webkul\Customer\Notifications\CustomerResetPassword; use Webkul\Customer\Contracts\Customer as CustomerContract; +use Illuminate\Support\Facades\Storage; use Webkul\Customer\Database\Factories\CustomerAddressFactory; class Customer extends Authenticatable implements CustomerContract, JWTSubject @@ -47,6 +48,26 @@ class Customer extends Authenticatable implements CustomerContract, JWTSubject 'remember_token', ]; + /** + * Get image url for the customer image. + */ + public function image_url() + { + if (! $this->image) { + return; + } + + return Storage::url($this->image); + } + + /** + * Get image url for the customer profile. + */ + public function getImageUrlAttribute() + { + return $this->image_url(); + } + /** * Get the customer full name. */ diff --git a/packages/Webkul/Customer/src/Repositories/CustomerRepository.php b/packages/Webkul/Customer/src/Repositories/CustomerRepository.php index 1e853476a..864670709 100755 --- a/packages/Webkul/Customer/src/Repositories/CustomerRepository.php +++ b/packages/Webkul/Customer/src/Repositories/CustomerRepository.php @@ -3,6 +3,7 @@ namespace Webkul\Customer\Repositories; use Webkul\Core\Eloquent\Repository; +use Illuminate\Support\Facades\Storage; class CustomerRepository extends Repository { @@ -48,4 +49,40 @@ class CustomerRepository extends Repository return false; } + + /** + * Upload customer's images. + * + * @param array $data + * @param \Webkul\Customer\Contracts\Customer $customer + * @param string $type + * @return void + */ + public function uploadImages($data, $customer, $type = "image") + { + if (isset($data[$type])) { + $request = request(); + + foreach ($data[$type] as $imageId => $image) { + $file = $type . '.' . $imageId; + $dir = 'customer/' . $customer->id; + + if ($request->hasFile($file)) { + if ($customer->{$type}) { + Storage::delete($customer->{$type}); + } + + $customer->{$type} = $request->file($file)->store($dir); + $customer->save(); + } + } + } else { + if ($customer->{$type}) { + Storage::delete($customer->{$type}); + } + + $customer->{$type} = null; + $customer->save(); + } + } } \ No newline at end of file diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/partials/sidemenu.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/partials/sidemenu.blade.php index d0bab2b38..ad99a8c06 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/partials/sidemenu.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/partials/sidemenu.blade.php @@ -1,8 +1,14 @@
-
- {{ substr(auth('customer')->user()->first_name, 0, 1) }} -
+ @if (auth('customer')->user()->image) +
+ {{ auth('customer')->user()->first_name }} +
+ @else +
+ {{ substr(auth('customer')->user()->first_name, 0, 1) }} +
+ @endif
{{ auth('customer')->user()->first_name . ' ' . auth('customer')->user()->last_name}}
{{ auth('customer')->user()->email }}
diff --git a/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/profile/edit.blade.php b/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/profile/edit.blade.php index 518cca229..8f729d718 100644 --- a/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/profile/edit.blade.php +++ b/packages/Webkul/Velocity/src/Resources/views/shop/customers/account/profile/edit.blade.php @@ -16,7 +16,8 @@
+ action="{{ route('customer.profile.store') }}" + enctype="multipart/form-data">