feat(user): menambahkan upload ktp ,merubah logika trait untuk upload attachment,menyesuaikan avatar di header
This commit is contained in:
parent
091df5d9e9
commit
90b154f18a
@ -23,7 +23,7 @@ public function index(): View
|
||||
{
|
||||
return view('pages.admin.master.user.index', [
|
||||
'pageTitle' => 'Pengguna',
|
||||
'users' => User::with(['biography', 'attachment'])
|
||||
'users' => User::with(['biography', 'attachments'])
|
||||
->barbershop()
|
||||
->latest()
|
||||
->get(),
|
||||
@ -52,7 +52,8 @@ public function store(UserRequest $request): RedirectResponse
|
||||
Biography::create(array_merge($validatedData, [
|
||||
'user_id' => $newUser->id,
|
||||
]), $validatedData);
|
||||
$this->uploadAttachment($validatedData['avatar'], 'users', User::class, $newUser->id);
|
||||
$this->uploadAttachment($validatedData['avatar'], 'users', User::class, $newUser->id, 'avatar');
|
||||
$this->uploadAttachment($validatedData['ktp'], 'users', User::class, $newUser->id, 'ktp');
|
||||
});
|
||||
|
||||
notify()->success('Data berhasil ditambahkan', 'Berhasil');
|
||||
@ -90,11 +91,15 @@ public function update(User $user, UserRequest $request): RedirectResponse
|
||||
'base_salary',
|
||||
'sop',
|
||||
'avatar',
|
||||
'ktp',
|
||||
])
|
||||
->toArray();
|
||||
$user->biography()->update($biographyData);
|
||||
if (isset($validatedData['avatar'])) {
|
||||
$this->uploadAttachment($validatedData['avatar'], 'users', User::class, $user->id);
|
||||
$this->uploadAttachment($validatedData['avatar'], 'users', User::class, $user->id, 'avatar');
|
||||
}
|
||||
if (isset($validatedData['ktp'])) {
|
||||
$this->uploadAttachment($validatedData['ktp'], 'users', User::class, $user->id, 'ktp');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@ -41,6 +41,9 @@ public function rules(): array
|
||||
'avatar' => $isEdit
|
||||
? ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048']
|
||||
: ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
|
||||
'ktp' => $isEdit
|
||||
? ['nullable', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048']
|
||||
: ['required', 'image', 'mimes:jpeg,png,jpg,gif,svg', 'max:2048'],
|
||||
'gender' => ['required', Rule::in(['1', '2'])],
|
||||
];
|
||||
|
||||
|
||||
@ -17,6 +17,7 @@ class Attachment extends Model
|
||||
'name',
|
||||
'extension',
|
||||
'size',
|
||||
'type',
|
||||
];
|
||||
|
||||
public function getFormattedAttachmentAttribute(): string
|
||||
|
||||
@ -11,7 +11,7 @@
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
@ -99,8 +99,8 @@ public function cash(): HasMany
|
||||
return $this->hasMany(Cash::class);
|
||||
}
|
||||
|
||||
public function attachment(): MorphOne
|
||||
public function attachments(): MorphMany
|
||||
{
|
||||
return $this->morphOne(Attachment::class, 'attachmentable');
|
||||
return $this->morphMany(Attachment::class, 'attachmentable');
|
||||
}
|
||||
}
|
||||
|
||||
@ -10,9 +10,9 @@
|
||||
|
||||
trait UploadAttachment
|
||||
{
|
||||
public function uploadAttachment(UploadedFile|string $file, string $path, string $classModel, string $featureId): void
|
||||
public function uploadAttachment(UploadedFile|string $file, string $path, string $classModel, string $featureId, string $type): void
|
||||
{
|
||||
DB::transaction(function () use ($file, $path, $classModel, $featureId) {
|
||||
DB::transaction(function () use ($file, $path, $classModel, $featureId, $type) {
|
||||
if (is_string($file) && strpos($file, 'data:image') === 0) {
|
||||
$imageData = explode(',', $file)[1];
|
||||
$image = base64_decode($imageData);
|
||||
@ -23,36 +23,36 @@ public function uploadAttachment(UploadedFile|string $file, string $path, string
|
||||
$date = now()->format('Y-m-d');
|
||||
Storage::disk('public')->put($path.'/'.$date.'/'.$fullFileName, $image);
|
||||
|
||||
Attachment::create([
|
||||
'attachmentable_id' => $featureId,
|
||||
'attachmentable_type' => $classModel,
|
||||
'name' => $fileName,
|
||||
'extension' => $extension,
|
||||
'size' => strlen($image),
|
||||
]);
|
||||
Attachment::updateOrCreate(
|
||||
[
|
||||
'attachmentable_id' => $featureId,
|
||||
'attachmentable_type' => $classModel,
|
||||
'type' => $type,
|
||||
],
|
||||
[
|
||||
'name' => $fileName,
|
||||
'extension' => $extension,
|
||||
'size' => strlen($image),
|
||||
]
|
||||
);
|
||||
} elseif ($file instanceof UploadedFile) {
|
||||
$existingAttachment = Attachment::where('attachmentable_id', $featureId)
|
||||
->where('attachmentable_type', $classModel)
|
||||
->first();
|
||||
|
||||
if ($existingAttachment) {
|
||||
$oldFilePath = $path.'/'.$existingAttachment->created_at->format('Y-m-d').'/'.$existingAttachment->name.'.'.$existingAttachment->extension;
|
||||
Storage::disk('public')->delete($oldFilePath);
|
||||
$existingAttachment->delete();
|
||||
}
|
||||
|
||||
$date = now()->format('Y-m-d');
|
||||
$extension = $file->getClientOriginalExtension();
|
||||
$fileName = Str::uuid();
|
||||
$file->storeAs($path.'/'.$date, $fileName.'.'.$extension, 'public');
|
||||
|
||||
Attachment::create([
|
||||
'attachmentable_id' => $featureId,
|
||||
'attachmentable_type' => $classModel,
|
||||
'name' => $fileName,
|
||||
'extension' => $extension,
|
||||
'size' => $file->getSize(),
|
||||
]);
|
||||
Attachment::updateOrCreate(
|
||||
[
|
||||
'attachmentable_id' => $featureId,
|
||||
'attachmentable_type' => $classModel,
|
||||
'type' => $type,
|
||||
],
|
||||
[
|
||||
'name' => $fileName,
|
||||
'extension' => $extension,
|
||||
'size' => $file->getSize(),
|
||||
]
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@ public function up(): void
|
||||
Schema::create('attachments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->nullableMorphs('attachmentable');
|
||||
$table->string('type', 20);
|
||||
$table->string('name', 50);
|
||||
$table->string('extension', 10);
|
||||
$table->string('size', 10);
|
||||
|
||||
@ -51,6 +51,7 @@ public function run(): void
|
||||
'name' => $fileName,
|
||||
'extension' => $file->getExtension(),
|
||||
'size' => $file->getSize(),
|
||||
'type' => 'avatar',
|
||||
]);
|
||||
|
||||
$path = 'users/'.now()->format('Y-m-d');
|
||||
|
||||
@ -16,7 +16,10 @@ $(document).ready(function () {
|
||||
gender: button.data("gender"),
|
||||
sop: button.data("sop"),
|
||||
avatar: button.data("avatar"),
|
||||
ktp: button.data("ktp"),
|
||||
};
|
||||
|
||||
console.log(data);
|
||||
var genderBadge = '';
|
||||
if (data.gender === 1) {
|
||||
genderBadge = '<span class="badge bg-success">Laki-laki</span>';
|
||||
@ -34,5 +37,6 @@ $(document).ready(function () {
|
||||
$('#user-address').text(data.address);
|
||||
$('#user-gender').html(genderBadge);
|
||||
$('#user-sop').text(data.sop);
|
||||
$('#user-ktp').attr('src', data.ktp);
|
||||
});
|
||||
});
|
||||
|
||||
@ -148,6 +148,19 @@ class="form-control rupiah-format @error('base_salary') is-invalid @enderror" id
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-6 position-relative">
|
||||
<label class="form-label" for="ktp">KTP
|
||||
<span class="form-span"><i>(.jpeg,.png,.jpg,.gif,.svg)</i></span>
|
||||
</label>
|
||||
<input name="ktp" class="form-control @error('ktp') is-invalid @enderror" id="ktp"
|
||||
type="file" value="{{ old('ktp') }}"
|
||||
accept="image/jpeg,image/png,image/jpg,image/gif,image/svg">
|
||||
@error('ktp')
|
||||
<div class="invalid-feedback">
|
||||
{{ $message }}
|
||||
</div>
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-4 position-relative">
|
||||
<label class="form-label" for="gender">Jenis Kelamin
|
||||
</label>
|
||||
<select name="gender" class="form-control @error('gender') is-invalid @enderror select2"
|
||||
@ -162,7 +175,7 @@ class="form-control rupiah-format @error('base_salary') is-invalid @enderror" id
|
||||
</div>
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-6 position-relative">
|
||||
<div class="col-lg-4 position-relative">
|
||||
<label class="form-label" for="barbershop">Barbershop
|
||||
</label>
|
||||
<select name="barbershop" class="form-control @error('barbershop') is-invalid @enderror select2"
|
||||
@ -180,7 +193,7 @@ class="form-control rupiah-format @error('base_salary') is-invalid @enderror" id
|
||||
</div>
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-6 position-relative">
|
||||
<div class="col-lg-4 position-relative">
|
||||
<label class="form-label" for="role">Role
|
||||
</label>
|
||||
<select name="role" class="form-control @error('role') is-invalid @enderror select2"
|
||||
|
||||
@ -147,6 +147,19 @@ class="form-control rupiah-format @error('base_salary') is-invalid @enderror" id
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-6 position-relative">
|
||||
<label class="form-label" for="ktp">KTP
|
||||
<span class="form-span"><i>(.jpeg,.png,.jpg,.gif,.svg, Opsional)</i></span>
|
||||
</label>
|
||||
<input name="ktp" class="form-control @error('ktp') is-invalid @enderror" id="ktp"
|
||||
type="file" value="{{ old('ktp') }}"
|
||||
accept="image/jpeg,image/png,image/jpg,image/gif,image/svg">
|
||||
@error('ktp')
|
||||
<div class="invalid-feedback">
|
||||
{{ $message }}
|
||||
</div>
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-4 position-relative">
|
||||
<label class="form-label" for="gender">Jenis Kelamin
|
||||
</label>
|
||||
<select name="gender" class="form-control @error('gender') is-invalid @enderror select2"
|
||||
@ -161,7 +174,7 @@ class="form-control rupiah-format @error('base_salary') is-invalid @enderror" id
|
||||
</div>
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-6 position-relative">
|
||||
<div class="col-lg-4 position-relative">
|
||||
<label class="form-label" for="barbershop">Barbershop
|
||||
</label>
|
||||
<select name="barbershop" class="form-control @error('barbershop') is-invalid @enderror select2"
|
||||
@ -179,7 +192,7 @@ class="form-control rupiah-format @error('base_salary') is-invalid @enderror" id
|
||||
</div>
|
||||
@enderror
|
||||
</div>
|
||||
<div class="col-lg-6 position-relative">
|
||||
<div class="col-lg-4 position-relative">
|
||||
<label class="form-label" for="role">Role
|
||||
</label>
|
||||
<select name="role" class="form-control @error('role') is-invalid @enderror select2"
|
||||
|
||||
@ -93,7 +93,8 @@
|
||||
data-address="{{ $user->biography->address ?? '-' }}"
|
||||
data-gender="{{ $user->biography->gender ?? '-' }}"
|
||||
data-sop="{{ $user->sop ?? '-' }}"
|
||||
data-avatar="{{ Storage::url("users/{$user->attachment->formatted_attachment}") }}">
|
||||
data-avatar="{{ optional($user->attachments()->where('type', 'avatar')->first())->formatted_attachment ? Storage::url('users/' . optional($user->attachments()->where('type', 'avatar')->first())->formatted_attachment) : '' }}"
|
||||
data-ktp="{{ optional($user->attachments()->where('type', 'ktp')->latest()->first())->formatted_attachment ? Storage::url('users/' . optional($user->attachments()->where('type', 'ktp')->latest()->first())->formatted_attachment) : '' }}">
|
||||
<i data-feather="eye"></i>
|
||||
</a>
|
||||
</li>
|
||||
@ -169,12 +170,13 @@
|
||||
<td class="text-start"><span id="user-gender"></span></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="text-start">Sop</td>
|
||||
<td class="text-start">-></td>
|
||||
<td class="text-start"><span id="user-sop"></span></td>
|
||||
<td class="text-start" colspan="3"><span id="user-sop"></span></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="mt-3">
|
||||
<img id="user-ktp" src="" alt="ktp">
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -64,7 +64,7 @@
|
||||
</li>
|
||||
<li class="profile-nav onhover-dropdown pe-0 py-0">
|
||||
<div class="media profile-media">
|
||||
<img src="{{ Storage::url('users/' . Auth::user()->attachment->formatted_attachment) }}"
|
||||
<img src="{{ Storage::url('users/' . Auth::user()->attachments()->first()->formatted_attachment) }}"
|
||||
alt="image">
|
||||
<div class="media-body">
|
||||
<span>{{ Auth::user()->biography->full_name }}</span>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user