Select the first 10 rows - Laravel Eloquent(选择前 10 行 - Laravel Eloquent)
问题描述
到目前为止,我有以下模型:
So far I have the following model:
class Listing extends Eloquent {
//Class Logic HERE
}
我想要一个基本函数来检索表列表"的前 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.
推荐答案
首先你可以使用分页器.这很简单:
First you can use a Paginator. This is as simple as:
$allUsers = User::paginate(15);
$someUsers = User::where('votes', '>', 100)->paginate(15);
变量将包含一个分页器类的实例.您的所有数据都将存储在 data
键下.
The variables will contain an instance of Paginator class. all of your data will be stored under data
key.
或者您可以执行以下操作:
Or you can do something like:
旧版本的 Laravel.
Old versions Laravel.
Model::all()->take(10)->get();
较新版本的 Laravel.
Newer version Laravel.
Model::all()->take(10);
有关更多阅读,请考虑以下链接:
For more reading consider these links:
- 分页文档
- 将数据传递给视图
- Eloquent 基本用法
- 备忘单
这篇关于选择前 10 行 - Laravel Eloquent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:选择前 10 行 - Laravel Eloquent


- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01