Edit File: SocialCreate.php
<?php namespace App\Livewire\Admin\Social; use App\Models\Social; 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 SocialCreate extends Component { use WithFileUploads; #[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() { $this->max_order = Social::count() + 1; $this->order = $this->max_order; } public function store() { $this->validate(); $social = new Social(); if($this->photo) { $extension = $this->photo->getClientOriginalExtension(); $newname = $this->name . ".$extension"; $this->photo->storeAs("social", $newname); $logo_path = "/storage/social/$newname"; $social->logo = $logo_path; } if($this->order != $this->max_order) { $get_current_social_based_on_order = Social::where("order", $this->order)->first(); $get_all_social_with_higher_order = Social::where('order', ">=", $this->order)->get(); foreach($get_all_social_with_higher_order as $high) { $high->order = $high->order + 1; $high->save(); } $social->order = $this->order; } else { $social->order = $this->order; } $social->name = $this->name; $social->display_text = $this->display_text; $social->link = $this->link; $social->save(); History::create([ 'name' => Auth::user()->name, 'page' => 'Social', 'title' => Auth::user()->name . ' Menambahkan Sosmed Baru ' . $social->name, 'status' => 'success', ]); Session::flash('status', 'success'); Session::flash('message', 'Berhasil Menambah Social Media'); return redirect()->route("admin.social.index"); } #[Layout('layouts.admin')] public function render() { return view('livewire.admin.social.social-create'); } }
Back