laravel - 如何在 laravel eloquent 中选择特定列

标签 laravel laravel-5 orm eloquent

假设表中有 7 列,我只想选择其中两列,如下所示

SELECT `name`,`surname` FROM `table` WHERE `id` = '1';

在 laravel eloquent 模型中,它可能看起来像这样

Table::where('id', 1)->get();

但我猜这个表达式会选择 id 等于 1 的所有列,而我只想要两列(姓名、姓氏)。如何只选择两列?

最佳答案

你可以这样做:

Table::select('name','surname')->where('id', 1)->get();

关于laravel - 如何在 laravel eloquent 中选择特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38172857/

相关文章:

php - ValidatePostSize.php 第 22 行 laravel 中的 PostTooLargeException

php - Laravel 5.3 - 以 orWhere 开头的查询生成器

php - Laravel 查询生成器中 FORCE INDEX FOR JOIN 的等价物

laravel - 在 Laravel 5 Controller 中找不到类 'App\Http\Controllers\DB'

java - Hibernate注解@ManyToMany

php - 如何与Yii2 faker数据库关联

php - 未找到基表或 View : 1146 Table 'xyz.testimonials' doesn't exist (SQL: select * from `testimonials` )

php - 如何修复 Laravel 5.7 中删除按钮不起作用的问题?

php - 在 Laravel 7 中返回响应图像

java - 在 JPA @OneToMany 关系中更新 Set 元素的正确方法?