ride = $ride; if ($review) { $this->review = $review; $this->isEditing = true; $this->rating = $review->rating; $this->title = $review->title; $this->content = $review->content; } } /** * Save or update the review */ public function save() { // Check if user is authenticated if (!Auth::check()) { $this->message = 'You must be logged in to submit a review.'; return; } // Rate limiting $key = 'review_' . Auth::id(); if (RateLimiter::tooManyAttempts($key, 5)) { // 5 attempts per minute $this->message = 'Please wait before submitting another review.'; return; } RateLimiter::hit($key); // Validate input $this->validate(); try { if ($this->isEditing) { // Check if user can edit this review if (!$this->review || $this->review->user_id !== Auth::id()) { $this->message = 'You cannot edit this review.'; return; } // Update existing review $this->review->update([ 'rating' => $this->rating, 'title' => $this->title, 'content' => $this->content, 'status' => ReviewStatus::PENDING, ]); $this->message = 'Review updated successfully. It will be visible after moderation.'; } else { // Check if user already reviewed this ride if (!$this->ride->canBeReviewedBy(Auth::user())) { $this->message = 'You have already reviewed this ride.'; return; } // Create new review Review::create([ 'ride_id' => $this->ride->id, 'user_id' => Auth::id(), 'rating' => $this->rating, 'title' => $this->title, 'content' => $this->content, 'status' => ReviewStatus::PENDING, ]); $this->message = 'Review submitted successfully. It will be visible after moderation.'; // Reset form $this->reset(['rating', 'title', 'content']); } $this->dispatch('review-saved'); } catch (\Exception $e) { $this->message = 'An error occurred while saving your review. Please try again.'; } } /** * Reset the form */ public function resetForm() { $this->reset(['rating', 'title', 'content', 'message']); if ($this->review) { $this->rating = $this->review->rating; $this->title = $this->review->title; $this->content = $this->review->content; } } /** * Render the component */ public function render() { return view('livewire.ride-review-component'); } }