Skip to content

Commit

Permalink
WIP: Fix Delete Slide (#990)
Browse files Browse the repository at this point in the history
Co-authored-by: ahmad afandi <ahmad.afandi85@gmail.com>
  • Loading branch information
arifpriadi and pandigresik authored Aug 26, 2024
1 parent 1e6fd57 commit c1c7de9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
25 changes: 9 additions & 16 deletions app/Http/Controllers/Setting/SlideController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,9 @@ public function store(SlideRequest $request)

if ($request->hasFile('gambar')) {
$file = $request->file('gambar');
$fileName = $file->getClientOriginalName();
$path = 'storage/slide/';
$file->move($path, $fileName);

$input['gambar'] = $path.$fileName;
$fileName = $file->hashName();
$path = $file->storeAs('public/slide', $fileName);
$input['gambar'] = str_replace('public/', 'storage/', $path);
}
Slide::create($input);
} catch (\Exception $e) {
Expand All @@ -94,15 +92,15 @@ public function store(SlideRequest $request)

public function show(Slide $slide)
{
$page_title = 'Detail Slide :'.$slide->judul;
$page_title = 'Detail Slide :' . $slide->judul;

return view('setting.slide.show', compact('page_title', 'page_description', 'slide'));
}

public function edit(Slide $slide)
{
$page_title = 'Slide';
$page_description = 'Ubah Slide : '.$slide->judul;
$page_description = 'Ubah Slide : ' . $slide->judul;

return view('setting.slide.edit', compact('page_title', 'page_description', 'slide'));
}
Expand All @@ -114,12 +112,9 @@ public function update(SlideRequest $request, Slide $slide)

if ($request->hasFile('gambar')) {
$file = $request->file('gambar');
$fileName = $file->getClientOriginalName();
$path = 'storage/slide/';
$file->move($path, $fileName);
unlink(base_path('public/'.$slide->gambar));

$input['gambar'] = $path.$fileName;
$fileName = $file->hashName();
$path = $file->storeAs('public/slide', $fileName);
$input['gambar'] = str_replace('public/', 'storage/', $path);
}
$slide->update($input);
} catch (\Exception $e) {
Expand All @@ -134,9 +129,7 @@ public function update(SlideRequest $request, Slide $slide)
public function destroy(Slide $slide)
{
try {
if ($slide->delete()) {
unlink(base_path('public/'.$slide->gambar));
}
$slide->delete();
} catch (\Exception $e) {
report($e);

Expand Down
22 changes: 22 additions & 0 deletions app/Models/Slide.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

namespace App\Models;

use Illuminate\Support\Facades\Log;
use Illuminate\Database\Eloquent\Model;

class Slide extends Model
Expand All @@ -42,4 +43,25 @@ class Slide extends Model
'judul',
'deskripsi',
];

public static function booted()
{
static::updating(function ($model) {
static::deleteImg($model);
});

static::deleting(function ($model) {
static::deleteImg($model, true);
});
}

protected static function deleteImg($model, $deleting = false)
{
if ($model->isDirty('gambar') || $deleting) {
$slidePath = public_path($model->getOriginal('gambar'));
if (file_exists($slidePath)) {
unlink($slidePath);
}
}
}
}
1 change: 1 addition & 0 deletions catatan_rilis.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Terima kasih pada @uddinmtm telah ikut berkontribusi.
3. [#1002](https://github.com/OpenSID/OpenDK/issues/1002) Fix Error Statistik Pendidikan
4. [#1008](https://github.com/OpenSID/OpenDK/issues/1008) Perbaikan error tidak bisa mengirim keluhan di sikema.
5. [#985](https://github.com/OpenSID/OpenDK/issues/985) Perbaikan navigasi default
6. [#990](https://github.com/OpenSID/OpenDK/issues/990) Perbaikan error ketika hapus slide tanpa gambar

#### TEKNIS

Expand Down

0 comments on commit c1c7de9

Please sign in to comment.