mirror of
https://github.com/pacnpal/thrillwiki_laravel.git
synced 2025-12-20 09:11:10 -05:00
2.7 KiB
2.7 KiB
Review System Models
Review Model
Represents user reviews for rides in the system.
Properties
id(int) - Primary keyride_id(int) - Foreign key to rides tableuser_id(int) - Foreign key to users tablerating(int) - Rating from 1 to 5title(string, nullable) - Optional review titlecontent(text) - Review contentstatus(enum) - ReviewStatus enum valuemoderated_at(timestamp) - When review was moderatedmoderated_by(int) - Foreign key to users table (moderator)helpful_votes_count(int) - Counter cache for helpful votes
Relationships
ride- BelongsTo relationship to Ride modeluser- BelongsTo relationship to User modelmoderator- BelongsTo relationship to User modelhelpfulVotes- HasMany relationship to HelpfulVote model
Scopes
pending()- Reviews awaiting moderationapproved()- Approved reviewsrejected()- Rejected reviewsbyRide()- Filter by ridebyUser()- Filter by user
Methods
approve()- Approve the reviewreject()- Reject the reviewmoderate()- General moderation methodtoggleHelpfulVote()- Toggle helpful vote from a user
HelpfulVote Model
Represents users marking reviews as helpful.
Properties
id(int) - Primary keyreview_id(int) - Foreign key to reviews tableuser_id(int) - Foreign key to users tablecreated_at(timestamp) - When vote was cast
Relationships
review- BelongsTo relationship to Review modeluser- BelongsTo relationship to User model
Methods
toggle()- Toggle vote status
Database Considerations
Indexes
-
Reviews Table:
- Primary Key: id
- Foreign Keys: ride_id, user_id, moderated_by
- Composite: [ride_id, user_id] (unique)
- Indexes: [ride_id, status], [user_id, created_at], status
-
Helpful Votes Table:
- Primary Key: id
- Foreign Keys: review_id, user_id
- Composite: [review_id, user_id] (unique)
- Indexes: [user_id, created_at]
Constraints
-
Reviews:
- One review per ride per user
- Rating must be between 1 and 5
- Status must be valid enum value
- Content is required
-
Helpful Votes:
- One vote per review per user
- Cascading deletes with review
Usage Examples
// Create a review
$review = Review::create([
'ride_id' => $ride->id,
'user_id' => Auth::id(),
'rating' => 5,
'title' => 'Great ride!',
'content' => 'This was amazing...',
]);
// Toggle helpful vote
$review->toggleHelpfulVote(Auth::id());
// Moderate a review
$review->moderate(ReviewStatus::APPROVED, Auth::id());
// Get ride's approved reviews
$reviews = $ride->reviews()->approved()->latest()->get();
// Get user's helpful votes
$votes = $user->helpfulVotes()->with('review')->get();