Laravel: Output Value As A Star Rating
Solution 1:
To output the stars in your templates you can do the following in the example below. But first a little note on your variable usage: Try to make any arrays of reviews plural and a single review singular. So $reviews
for all reviews and a single review $review
. This makes your code more readable and understandable for you and others.
So your $review->rating
property is number. When the number is 3 you want to show 3 stars with the correct styling.
Use a loop to output 5 stars. Inside that loop see if the index of current star is lower or equal than the rating value. When it is, it means that the current star has to be part of the rating. And if it is not, it means that this star should be grayed out or styled differently.
This will result in all the stars that should be gold to have the star--gold
class on it which can be styled accordingly.
@if(count($reviews) > 1)
@foreach($reviewsas$review)
<div class= "well">
<h3>
<ahref="/reviews/{{ $review->title }}">{{ $review->title }}</a>
@for ($i = 0; $i < 5; $i++)
@if ($i < $review->rating)
<span class="star star--gold"></span>
@else
<span class="star"></span>
@endif
@endfor
</h3>
<small>{{ $review->created_at }}</small>
<br>
<small>{{ $review->body }}</small>
<br>
<br>
</div>
@endforeach
@else
Post a Comment for "Laravel: Output Value As A Star Rating"