Edit File: VideoShow.php
<?php namespace App\Livewire\Show; use App\Models\Comment; use Livewire\Component; use Livewire\Attributes\Layout; use Livewire\Attributes\Validate; use App\Models\Anime as AnimeModel; use App\Models\Video as VideoModel; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; class VideoShow extends Component { public $video; public $anime; public $random_anime; public $next_video; public $prev_video; #[Validate("string|max:500|required", message: ['max' => 'Komentar Tidak Boleh Lebih Dari 500 Karakter', 'required' => "Komentar Tidak Boleh Kosong"])] public string $description; public bool $priority = false; public bool $commentState = false; public $comments; public int $max_comment; public int $load_item = 1; public $query; public function mount($slug, $order) { $this->anime = AnimeModel::where("slug", $slug)->first(); if($this->anime === null) { abort(404, 'Anime not found'); } $this->video = $this->anime->videos()->where("order", $order)->first(); if (!$this->video) { $this->video = $this->anime->videos()->orderByDesc("order")->first(); return redirect()->route("video.show", [$this->anime->slug, $this->video->order]); } $this->random_anime = AnimeModel::where("id", "!=", $this->anime->id)->where("is_film", $this->anime->is_film)->inRandomOrder()->limit(6)->get(); $this->max_comment = count($this->video->comments); $this->refreshComment(); if ($this->video->order > 1) { $this->prev_video = $this->video->order - 1; } else { $this->prev_video = "#"; } if ($this->video->order < count($this->anime->videos)) { $this->next_video = $this->video->order + 1; } else { $this->next_video = "#"; } } public function refreshComment() { $this->comments = $this->video->comments()->with("user")->orderByDesc("created_at")->limit($this->load_item)->get(); } public function togglePriority() { $this->priority = !$this->priority; } public function toggleComment() { $this->commentState = !$this->commentState; } public function loadMoreComments() { $this->load_item += 5; $this->refreshComment(); } public function comment() { if (Auth::check()) { $this->validate(); $comment = new Comment(); $comment->user_id = Auth::id(); $comment->video_id = $this->video->id; $comment->is_urgent = $this->priority; $comment->description = $this->description; $comment->save(); $this->description = ""; $this->refreshComment(); } else { return redirect()->route("register"); } } #[\Livewire\Attributes\On('incrementViewCount')] public function incrementViewCount() { // Check if the video has been viewed in this session if (!Session::has('viewed_video_' . $this->video->id)) { // Increment the view count $this->video->increment('view_count'); $this->anime->increment("view_count"); $this->anime->increment("view_daily"); // Mark video as viewed in the session Session::put('viewed_video_' . $this->video->id, true); } } #[\Livewire\Attributes\On('videoEnded')] public function videoEnded() { if ($this->next_video != "#") { return redirect()->route("video.show", ['slug' => $this->anime->slug, 'order' => $this->next_video]); } } public function searchEpisode() { $video = $this->anime->videos()->where("order", $this->query)->first(); if ($video) { return redirect()->route("video.show", ['slug' => $this->anime->slug, 'order' => $video->order]); } else { $this->reset('query'); $this->addError("episode_error", "Episode Tidak Ditemukan"); } } #[Layout("layouts.guest")] public function render() { return view('livewire.show.video-show'); } }
Back