【发布时间】:2019-10-13 07:57:47
【问题描述】:
有办法使用@foreach 将我的数据库中添加的最后一项显示到我的家吗?
@foreach(App\Models\Post::orderBy('created_at','DESC')->get() as $post)
<div class="fs-rp-item">
<div class="entry-image">
<!--<a href="#"><img src="/holy/images/blog/fs-thumb.jpg" alt="recent post"></a>-->
<a href="#"><img src="/uploads/post/{{$post->id}}/image/{{$post->file_1}}" alt="recent post"></a>
</div>
<div class="entry-rp">
<h4>
<a href="#">{{$post->title}}</a>
</h4>
<p class="read-more">
<a href="#">read the article</a>
</p>
</div>
</div>
@endforeach
【问题讨论】:
-
你还没有做过吗?
-
如果您只想显示最后一项,为什么要遍历循环?
-
我考虑过使用 Laravel eloquent 模型来获取 DB 上的最后一条记录。它的工作原理类似于 Model ::all()->last() 。试试这个 Post::all()->last() 。你可以在这里查看更多选项stackoverflow.com/questions/16549455/…
标签: laravel