Edit File: SocialEdit.php
<?php namespace App\Livewire\Admin\Social; use App\Models\Social; use App\Models\History; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; 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 SocialEdit extends Component { use WithFileUploads; public $social; public $oldphoto; #[Validate("image|nullable|max:1024")] public $photo; #[Validate("required|string|min:1|max:100")] public string $name; #[Validate("required|string|min:1|max:255")] public string $display_text; #[Validate("required|url|min:1|max:255")] public string $link; public $order; public $max_order; public function mount($id) { $this->social = Social::find($id); $this->oldphoto = $this->social->logo; $this->name = $this->social->name; $this->display_text = $this->social->display_text; $this->link = $this->social->link; $this->max_order = Social::count(); $this->order = $this->social->order; } public function update() { $this->validate(); if ($this->photo) { $old_storage_path = substr($this->social->logo, 9); Storage::delete($old_storage_path); $extension = $this->photo->getClientOriginalExtension(); $newname = Str::slug($this->name, "-") . ".$extension"; $this->photo->storeAs("social", $newname); $logo_path = "/storage/social/$newname"; $this->social->logo = $logo_path; } if ($this->order != $this->social->order) { $get_current_social_based_on_order = Social::where("order", $this->order)->first(); if ($get_current_social_based_on_order) { $get_current_social_based_on_order->order = $this->social->order; $get_current_social_based_on_order->save(); } $this->social->order = $this->order; } else { $this->social->order = $this->order; } $this->social->name = $this->name; $this->social->display_text = $this->display_text; $this->social->link = $this->link; $this->social->save(); History::create([ 'name' => Auth::user()->name, 'page' => 'Social', 'title' => Auth::user()->name . ' Mengupdate Sosmed ' . $this->social->name, 'status' => 'info', ]); Session::flash('status', 'success'); Session::flash('message', 'Berhasil Mengupdate ' . $this->social->name); return redirect()->route("admin.social.index"); } #[Layout("layouts.admin")] public function render() { return view('livewire.admin.social.social-edit'); } }
Back