<a href="@Url.Action("Create", "User")" class="btn btn-success" >
<span class="glyphicon glyphicon-shopping-cart"></span> سبد خرید شما
<span class="badge"> @ViewBag.BAGCOUNT</span>
</a>
这是我的代码,我想从数据库中选择前5名
答案 0 :(得分:0)
在您获得$posts
var而不是
Post::orderBy('created_at','asc')->get();
使用类似
的内容Post::orderBy('created_at','asc')->take(5)->get();
注意:这只是你如何致电帖子的假设
答案 1 :(得分:0)
你可以这样使用
Post::all()->take(5)->get();
答案 2 :(得分:0)
您可以使用模型
获取表格中的记录数量$count = ModelName::count();
前5名记录
ModelName::all()->take(5)->get();
对于paginatation
$data= ModelName::paginate(15);
答案 3 :(得分:0)
Fetch the 5 rows from the database by writing the select query with limit and then loop those columns in view page.
In Controller:
public function post_lists(){
$posts=\DB::table('posts_table')->limit(5)->orderby('postid','desc')->get();
return view('posts', compact('posts'));
}
In View page:
<h3>Top Members</h3>
<div class ="name">
<div class="col-md-2" id="content">
<?php $i=0; ?>
@foreach($posts as $post)
<h5><a href="#"<i class="glyphicon glyphicon-user"></i></a>{{ $post->user->name }} </h5>
@endforeach