Merge pull request #5235 from vivek-webkul/patch-45

[Fixed: #4787 Unable to upload profile picture.]
This commit is contained in:
Devansh 2021-10-14 14:35:57 +05:30 committed by GitHub
commit da58145dc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 122 additions and 5 deletions

View File

@ -50,7 +50,7 @@ class Category extends TranslatableModel implements CategoryContract
*/
public function image_url()
{
if (!$this->image) {
if (! $this->image) {
return;
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddImageColumnToCustomersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('customers', function (Blueprint $table) {
$table->string('image')->nullable()->after('email');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('customers', function (Blueprint $table) {
$table->dropColumn('image');
});
}
}

View File

@ -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']);

View File

@ -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.
*/

View File

@ -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();
}
}
}

View File

@ -1,8 +1,14 @@
<div class="customer-sidebar row no-margin no-padding">
<div class="account-details col-12">
<div class="customer-name col-12 text-uppercase">
{{ substr(auth('customer')->user()->first_name, 0, 1) }}
</div>
@if (auth('customer')->user()->image)
<div>
<img style="width:80px;height:80px;border-radius:50%;" src="{{ auth('customer')->user()->image_url }}" alt="{{ auth('customer')->user()->first_name }}"/>
</div>
@else
<div class="customer-name col-12 text-uppercase">
{{ substr(auth('customer')->user()->first_name, 0, 1) }}
</div>
@endif
<div class="col-12 customer-name-text text-capitalize text-break">{{ auth('customer')->user()->first_name . ' ' . auth('customer')->user()->last_name}}</div>
<div class="customer-email col-12 text-break">{{ auth('customer')->user()->email }}</div>
</div>

View File

@ -16,7 +16,8 @@
<form
method="POST"
@submit.prevent="onSubmit"
action="{{ route('customer.profile.store') }}">
action="{{ route('customer.profile.store') }}"
enctype="multipart/form-data">
<div class="account-table-content">
@csrf
@ -142,6 +143,23 @@
{!! view_render_event('bagisto.shop.customers.account.profile.edit.phone.after', ['customer' => $customer]) !!}
<div class="row image-container {!! $errors->has('image.*') ? 'has-error' : '' !!}">
<label class="col-12">
{{ __('admin::app.catalog.categories.image') }}
</label>
<div class="col-12">
<image-wrapper :button-label="'{{ __('admin::app.catalog.products.add-image-btn-title') }}'" input-name="image" :multiple="false" :images='"{{ $customer->image_url }}"'></image-wrapper>
<span class="control-error" v-if="{!! $errors->has('image.*') !!}">
@foreach ($errors->get('image.*') as $key => $message)
@php echo str_replace($key, 'Image', $message[0]); @endphp
@endforeach
</span>
</div>
</div>
{!! view_render_event('bagisto.shop.customers.account.profile.edit.image.after', ['customer' => $customer]) !!}
<div class="row">
<label class="col-12">
{{ __('velocity::app.shop.general.enter-current-password') }}