如何在laravel eloquent中选择特定的列 [英] How to select specific columns in laravel eloquent
本文介绍了如何在laravel eloquent中选择特定的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我在表中有 7 列,我只想选择其中的两列,就像这样
lets say I have 7 columns in table, and I want to select only two of them, something like this
SELECT `name`,`surname` FROM `table` WHERE `id` = '1';
在 laravel eloquent 模型中可能是这样的
In laravel eloquent model it may looks like this
Table::where('id', 1)->get();
但我猜这个表达式会选择所有 id 等于 1 的列,而我只想要两列(姓名、姓氏).如何只选择两列?
but I guess this expression will select ALL columns where id equals 1, and I want only two columns(name, surname). how to select only two columns?
推荐答案
你可以这样做:
Table::select('name','surname')->where('id', 1)->get();
这篇关于如何在laravel eloquent中选择特定的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文