I have a Score-Table with different ratings, strings and radio-buttons.
Now I want to loop through these. Normally I would go about solving it like this:
<table>
<tr>
<th>ID</th>
<th>Type</th>
<th>Comment</th>
</tr>
@foreach($scores as $score)
<tr>
<td>{{$score->id}}</td>
<td>{{$score->ratingradio}}</td>
<td>{{$score->ratingtext}}</td>
</tr>
@endforeach
</table>
But I don't only want the order to be reversed, but I also want the array to be sliced, so that it just outputs the last 20 Elements of the array.
I attempted solving it like this in my Controller:
$scores = Score::where('unit_id', $id)->where('created_at', '>', Carbon::now()->subDays(3))->get();
// Save all ratingtexts in an array
$comments = $scores->lists('ratingtext');
$commenttype = $scores->lists('ratingradio');
// Get the last 20 Elements of the Array
$comments = array_slice($comments, -20, 20, true);
// Reverse the array, to have the latest element first displayed
$comments = array_reverse($comments, true);
And then looping through the $comments. But I don't only want to display the comment, I also want to be able to display all Information regarding this Element. So preferably like the above method with eloquent, where I output $score->ratingtext, $score->ratingradio, $score-id, and whatever I want.
I tried just using
@foreach(array_reverse($scores) as $score)
Which obviously didn't work, because $scores is an object and it was expecting an array. How am I going to reverse loop through every score of my Scores Table?