选择前10行 - Laravel雄辩 [英] Select the first 10 rows - Laravel Eloquent
问题描述
到目前为止,我有以下模型:
So far I have the following model:
class Listing extends Eloquent {
}
我想要一个基本的函数来检索我的表列表的前10行,并将它们传递给这个视图(通过控制器?)。
I want a basic function that retrieves the first 10 rows of my table "listings" and passes them on to the view (via a controller?).
我知道这是一个非常基本的任务,但是我找不到一个简单的指南,显示基本的一组结果,同时详细说明模型,控制器和查看文件中需要的内容。
I know this a very basic task but I can't find a simple guide that actually explains step-by-step how to display a basic set of results, whilst detailing what is required in the model, controller and view files.
推荐答案
首先你可以使用一个Paginator。这很简单:
First you can use a Paginator. This is as simple as:
$allUsers = User::paginate(15);
$someUsers = User::where('votes', '>', 100)->paginate(15);
变量将包含一个Paginator类的实例。您的所有数据都将存储在数据
中。
The variables will contain an instance of Paginator class. all of your data will be stored under data
key.
或者您可以执行类似
Model::all()->take(10)->get();
有关更多阅读考虑以下链接:
For more reading consider these links:
- pagination docs
- passing data to views
- Eloquent basic usage
- A cheat sheet
这篇关于选择前10行 - Laravel雄辩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!