Edit File: AnimeEdit.php
<?php namespace App\Livewire\Admin\Anime; use App\Models\Tag; use App\Models\Type; use App\Models\Anime; use App\Models\Genre; use App\Models\Season; use App\Models\History; use App\Models\Nation; use Livewire\Component; use Illuminate\Support\Str; use Livewire\WithFileUploads; use Livewire\Attributes\Layout; use Livewire\Attributes\Validate; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; use Illuminate\Support\Facades\Storage; use Illuminate\Validation\Rule; class AnimeEdit extends Component { use WithFileUploads; public string $title; #[Validate("string|required|max:100")] public string $name; #[Validate("string|required|max:65525")] public string $short_description; #[Validate("image|nullable|mimes:png,jpg,jpeg,webp")] public $photo; #[Validate("image|nullable|mimes:png,jpg,jpeg,webp")] public $homeslider; #[Validate("string|nullable")] public string $total_episode = "-"; #[Validate("string|nullable")] public string $release_date = "-"; public int $nation_id; #[Validate("string|nullable")] public string $score = "- / -"; #[Validate("string|nullable")] public string $duration = "-"; #[Validate("required")] public int $season_id; #[Validate("required")] public int $genre_id; #[Validate("required")] public int $type_id; public $seasons; public $genres; public $types; public $nations; public $anime; public $oldhomeslider; public $oldphoto; #[Validate("bool|required")] public bool $is_hot = true; #[Validate("bool|required")] public bool $is_priority = false; #[Validate("bool|required")] public bool $is_completed = false; #[Validate("bool|required")] public bool $is_film = false; #[Validate("bool|required")] public bool $is_series = false; public array $tag_multi_ids; public $tags; protected function rules() { return [ 'title' => [ 'string', 'required', 'max:255', Rule::unique("animes", "title")->ignore($this->anime->id), ], ]; } public function mount($id) { $this->anime = Anime::find($id); $this->seasons = Season::orderByDesc("order")->get(); $this->genres = Genre::orderBy("name")->get(); $this->nations = Nation::orderBy("name")->get(); $this->types = Type::orderBy("name")->get(); $this->tags = Tag::orderBy("name")->get()->toArray(); $this->season_id = $this->anime->season_id; $this->genre_id = $this->anime->genre_id; $this->type_id = $this->anime->type_id; $this->nation_id = $this->anime->nation_id; $this->title = $this->anime->title; $this->name = $this->anime->name; $this->duration = $this->anime->duration; $this->short_description = $this->anime->short_description; $this->is_hot = $this->anime->is_hot; $this->is_priority = $this->anime->is_priority; $this->is_film = $this->anime->is_film; $this->is_series = $this->anime->is_series; $this->total_episode = $this->anime->total_episode; $this->release_date = $this->anime->release_date; $this->score = $this->anime->score; $this->season_id = $this->anime->season_id; $this->genre_id = $this->anime->genre_id; $this->type_id = $this->anime->type_id; $this->oldphoto = $this->anime->thumbnail; $this->oldhomeslider = $this->anime->home_image; $this->tag_multi_ids = $this->anime->tags()->pluck('tags.id')->toArray(); $this->is_completed = $this->anime->is_completed; } public function update() { $this->validate(); $sanitized_anime_name = $this->name; $sanitized_anime_name = str_replace("?", "", $sanitized_anime_name); $sanitized_anime_name = str_replace(":", "-", $sanitized_anime_name); if ($this->photo) { $delete_path = substr($this->anime->thumbnail, 9); Storage::delete($delete_path); $extension = $this->photo->getClientOriginalExtension(); $newname = $sanitized_anime_name . "-thumbnail.$extension"; $this->photo->storeAs("anime/" . $sanitized_anime_name, $newname); $storage_path = "/storage/anime/" . $sanitized_anime_name . "/$newname"; $this->anime->thumbnail = $storage_path; } if ($this->homeslider) { $delete_path2 = substr($this->anime->home_image, 9); Storage::delete($delete_path2); $extension = $this->homeslider->getClientOriginalExtension(); $newname = $sanitized_anime_name . "-slider.$extension"; $this->homeslider->storeAs("anime/" . $sanitized_anime_name, $newname); $storage_path = "/storage/anime/" . $sanitized_anime_name . "/$newname"; $this->anime->home_image = $storage_path; } $this->anime->title = $this->title; $this->anime->name = $this->name; $this->anime->short_description = $this->short_description; $this->anime->release_date = $this->release_date; $this->anime->score = $this->score; $this->anime->duration = $this->duration; $this->anime->current_episode = 0; $this->anime->total_episode = $this->total_episode; $this->anime->season_id = $this->season_id; $this->anime->genre_id = $this->genre_id; $this->anime->type_id = $this->type_id; $this->anime->is_film = $this->is_film; $this->anime->is_series = $this->is_series; $this->anime->nation_id = $this->nation_id; $this->anime->is_priority = $this->is_priority; $this->anime->is_hot = $this->is_hot; $this->anime->is_completed = $this->is_completed; $this->anime->slug = Str::slug($this->title); $this->anime->save(); $this->anime->tags()->sync($this->tag_multi_ids); History::create([ 'name' => Auth::user()->name, 'page' => 'Anime', 'title' => Auth::user()->name . ' Mengupdate Anime ' . $this->name, 'status' => 'info', ]); Session::flash('status', 'success'); Session::flash('message', 'Berhasil Mengupdate Anime Baru'); return redirect()->route("admin.anime.index"); } #[Layout("layouts.admin")] public function render() { return view('livewire.admin.anime.anime-edit'); } }
Back