在 Laravel 中使用 Eloquent 使用 Foreach 循环反转数组元素的顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23463663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reverse Order of Array Elements with Foreach Loop using Eloquent in Laravel
提问by LoveAndHappiness
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.
但是我不仅希望顺序颠倒,而且还希望对数组进行切片,以便它只输出数组的最后 20 个元素。
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.
然后循环遍历 $comments。但我不仅想显示评论,还希望能够显示有关此元素的所有信息。所以最好像上面的 eloquent 方法,我输出 $score->ratingtext, $score->ratingradio, $score-id, 以及任何我想要的。
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?
这显然不起作用,因为 $scores 是一个对象,它期待一个数组。我将如何反向循环我的分数表的每个分数?
回答by ollieread
Retrieving the last 20 items is quite easy.
检索最后 20 个项目非常容易。
$scores = Score::where('unit_id', $id)
->where('created_at', '>', Carbon::now()->subDays(3))
->orderBy('created_at', 'desc')
->take(20)
->get();
$scores = $scores->reverse();
Done.
完毕。
Just tell it to pull out the first 20 items that match your query, with a reversed order and then reverse the collection to get the proper order.
只需告诉它以相反的顺序取出与您的查询匹配的前 20 个项目,然后反转集合以获得正确的顺序。
回答by Abdur Rahman Turawa
You can as well easily do @foreach($scores->reverse() as $score)
你也可以轻松做到 @foreach($scores->reverse() as $score)
回答by Aurel
You can use:
您可以使用:
array_reverse($scores->toArray());
or Eloquent\Collection methods to keep the Model object
或 Eloquent\Collection 方法来保持 Model 对象
$scores->reverse();
Take a look at the Eloquent\Collection API.
回答by Oleg Shakhov
There are even easyest way:
甚至还有最简单的方法:
$scores = Score::where('unit_id', $id)
->where('created_at', '>', Carbon::now()->subDays(3))
->orderBy('created_at', 'desc')
->orderBy('id', 'asc')
->take(20)
->get();
回答by user1669496
Looking through the API, it seems you can use take()
to make this very simple. It behaves a little differently when running on query builder vs collection, so you'd want to get your collection first.
查看 API,您似乎可以使用它take()
来使这变得非常简单。在查询构建器与集合上运行时,它的行为略有不同,因此您希望首先获取您的集合。
$scores = Score::where('unit_id', $id)->where('created_at', '>', Carbon::now()->subDays(3))->get()->take(-20);
$scores = Score::where('unit_id', $id)->where('created_at', '>', Carbon::now()->subDays(3))->get()->take(-20);