Skip to content

Commit

Permalink
Display reviews on product page
Browse files Browse the repository at this point in the history
  • Loading branch information
mspeake161 committed Feb 18, 2025
1 parent b3e3c6b commit 60ffe6b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
8 changes: 8 additions & 0 deletions app/Http/Controllers/ReviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,12 @@ function add(Request $request) {
// return to product page with message
return redirect()->route('product.show', ['id' => $request['product_id']])->with('message', 'Review added successfully');
}
// returns a list of reviews for a product
static function getReviews($productId) {
// get all reviews for product
$reviews = Review::all()->where('product_id', $productId);
// TODO: could involve some sorting in the future
// return them
return $reviews;
}
}
14 changes: 12 additions & 2 deletions resources/views/pages/product.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
@endsection
@php
use App\Models\Product;
use App\Http\Controllers\ReviewController;
// add to the popularity value of the product on viewing
$product['popularity'] = $product['popularity'] + 1;
$product->save();
Expand Down Expand Up @@ -66,10 +67,19 @@
<br>
<p id="show_id"><strong>Item ID: </strong>{{$product['id']}}</p>
</section>

@php($reviews = ReviewController::getReviews($product->id))
<section id="review" class="content-section">
<p><strong>Reviews:</strong></p>
<p>Customer reviews will be displayed here.</p>
<!-- TODO: Probably want to rename classes and do some css to make look nice -->
<div class="something">
@foreach($reviews as $review)
<div class="review">
<h2>{{$review['title']}}</h2>
<p>{{$review['review']}}</p>
<p>{{$review['rating']}}/5</p>
</div>
@endforeach
</div>
</section>
</div>

Expand Down

0 comments on commit 60ffe6b

Please sign in to comment.