Edit File: SeasonCreate.php
<?php namespace App\Livewire\Admin\Season; use App\Models\Season; use App\Models\History; use Livewire\Component; use Illuminate\Support\Str; use Livewire\Attributes\Layout; use Livewire\Attributes\Validate; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; class SeasonCreate extends Component { #[Validate("required|string|max:100|unique:seasons,name")] public string $name; #[Validate("required|numeric")] public $order; public $max_order; public function mount() { $this->max_order = Season::count() + 1; $this->order = $this->max_order; } public function store() { $this->validate(); $season = new Season(); $season->name = $this->name; $season->slug = Str::slug(strtolower($this->name)); if($this->order != $this->max_order) { $get_current_season_based_on_order = Season::where("order", $this->order)->first(); $get_all_season_with_higher_order = Season::where('order', ">=", $this->order)->get(); foreach($get_all_season_with_higher_order as $high) { $high->order = $high->order + 1; $high->save(); } $season->order = $this->order; } else { $season->order = $this->order; } $season->save(); History::create([ 'name' => Auth::user()->name, 'page' => 'Season', 'title' => Auth::user()->name . ' Menambahkan Season Baru ' . $season->name, 'status' => 'success', ]); Session::flash('status', 'success'); Session::flash('message', 'Berhasil Menambah Season ' . $season->name); return redirect()->route("admin.season.index"); } #[Layout("layouts.admin")] public function render() { return view('livewire.admin.season.season-create'); } }
Back