問(wèn)題描述
到目前為止,我有以下模型:
So far I have the following model:
class Listing extends Eloquent {
//Class Logic HERE
}
我想要一個(gè)基本函數(shù)來(lái)檢索表列表"的前 10 行并將它們傳遞給視圖(通過(guò)控制器?).
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?).
我知道這是一項(xiàng)非常基本的任務(wù),但我找不到一個(gè)簡(jiǎn)單的指南來(lái)實(shí)際解釋如何顯示一組基本結(jié)果,同時(shí)詳細(xì)說(shuō)明模型、控制器和視圖文件中的要求.
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.
推薦答案
首先你可以使用分頁(yè)器.這很簡(jiǎn)單:
First you can use a Paginator. This is as simple as:
$allUsers = User::paginate(15);
$someUsers = User::where('votes', '>', 100)->paginate(15);
變量將包含一個(gè)分頁(yè)器類(lèi)的實(shí)例.您的所有數(shù)據(jù)都將存儲(chǔ)在 data
鍵下.
The variables will contain an instance of Paginator class. all of your data will be stored under data
key.
或者您可以執(zhí)行以下操作:
Or you can do something like:
舊版本的 Laravel.
Old versions Laravel.
Model::all()->take(10)->get();
較新版本的 Laravel.
Newer version Laravel.
Model::all()->take(10);
有關(guān)更多閱讀,請(qǐng)考慮以下鏈接:
For more reading consider these links:
- 分頁(yè)文檔
- 將數(shù)據(jù)傳遞給視圖
- Eloquent 基本用法
- 備忘單
這篇關(guān)于選擇前 10 行 - Laravel Eloquent的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!