Edit File: BannerEdit.php
<?php namespace App\Livewire\Admin\Banner; use App\Models\Banner; use App\Models\History; use Livewire\Component; use Livewire\WithFileUploads; use Livewire\Attributes\Layout; use Livewire\Attributes\Validate; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; class BannerEdit extends Component { use WithFileUploads; #[Validate("string|required|max:100")] public string $name; #[Validate("url|required|max:100")] public string $link; #[Validate("image|nullable|mimes:webp,gif")] public $image; #[Validate("required")] public bool $is_top = false; #[Validate("required")] public bool $is_bottom = false; #[Validate("required|numeric")] public int $order; public int $max_order; public $oldimage; public $banner; public function mount($id) { $this->max_order = Banner::count(); $this->banner = Banner::find($id); $this->order = $this->banner->order; $this->oldimage = $this->banner->image; $this->name = $this->banner->name; $this->link = $this->banner->link; $this->is_top = $this->banner->is_top; $this->is_bottom = $this->banner->is_bottom; } public function update() { $this->banner->name = $this->name; $this->banner->link = $this->link; if($this->image) { // Proses Gambar $extension = $this->image->getClientOriginalExtension(); $newname = $this->name . "-" . time() . ".$extension"; $this->image->storeAs("banner/$this->name", $newname); $storage_path = "/storage/banner/$this->name/$newname"; $this->banner->image = $storage_path; } if($this->order != $this->banner->order) { $banner_on_the_order = Banner::where("order", $this->order)->first(); if($banner_on_the_order) { $banner_on_the_order->order = $this->banner->order; $banner_on_the_order->save(); } } $this->banner->order = $this->order; $this->banner->is_top = $this->is_top; $this->banner->is_bottom = $this->is_bottom; $this->banner->save(); Session::flash('status', 'success'); Session::flash('message', 'Berhasil Mengupdate Banner Iklan'); History::create([ 'name' => Auth::user()->name, 'page' => 'Banners', 'title' => Auth::user()->name . ' Mengupdate Banner Iklan', 'status' => 'info', ]); return redirect()->route("admin.banner.index"); } #[Layout("layouts.admin")] public function render() { return view('livewire.admin.banner.banner-edit'); } }
Back