laravel 如何在laravel eloquent中选择特定的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38172857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select specific columns in laravel eloquent
提问by devnull Ψ
lets say I have 7 columns in table, and I want to select only two of them, something like this
假设我在表中有 7 列,我只想选择其中的两列,就像这样
SELECT `name`,`surname` FROM `table` WHERE `id` = '1';
In laravel eloquent model it may looks like this
在 Laravel eloquent 模型中,它可能看起来像这样
Table::where('id', 1)->get();
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?
但我猜这个表达式将选择 id 等于 1 的所有列,我只想要两列(姓名、姓氏)。如何只选择两列?
回答by Marcin Nabia?ek
You can do it like this:
你可以这样做:
Table::select('name','surname')->where('id', 1)->get();
回答by Utkarsh Pandey
Table::where('id', 1)->get(['name','surname']);
回答by Sivakumar Tadisetti
By using all() method we can select particular columns from table like as shown below.
通过使用 all() 方法,我们可以从表中选择特定的列,如下所示。
ModelName::all('column1', 'column2', 'column3');
Note: Laravel 5.4
注意:Laravel 5.4
回答by Sammie
You can also use find()
like this:
你也可以find()
这样使用:
ModelName::find($id, ['name', 'surname']);
The $id
variable can be an array in case you need to retrieve multiple instances of the model.
$id
如果您需要检索模型的多个实例,该变量可以是一个数组。
回答by user3888958
Also Model::all(['id'])->toArray()
it will only fetch id as array.
也Model::all(['id'])->toArray()
只会获取id作为数组。
回答by Mahesh Yadav
You first need to create a Model, that represent that Table and then use the below Eloquent way to fetch the data of only 2 fields.
您首先需要创建一个代表该表的模型,然后使用下面的 Eloquent 方式来获取仅 2 个字段的数据。
Model::where('id', 1)
->pluck('name', 'surname')
->all();
回答by aleXela
You can use get() as well as all()
您可以使用 get() 以及 all()
ModelName::where('a', 1)->get(['column1','column2']);
回答by bereket gebredingle
Also you can use pluck.
你也可以使用 pluck。
Model::where('id',1)->pluck('column1', 'column2');
回答by Josh Rumbut
You can use Table::select ('name', 'surname')->where ('id', 1)->get ()
.
您可以使用Table::select ('name', 'surname')->where ('id', 1)->get ()
.
Keep in mind that when selecting for only certain fields, you will have to make another query if you end up accessing those other fields later in the request (that may be obvious, just wanted to include that caveat). Including the id field is usually a good idea so laravel knows how to write back any updates you do to the model instance.
请记住,当仅选择某些字段时,如果您最终在请求中访问其他字段,则必须进行另一个查询(这可能很明显,只是想包括该警告)。包含 id 字段通常是一个好主意,因此 laravel 知道如何写回您对模型实例所做的任何更新。
回答by Jitender Sharma
To get the result of specific column from table,we have to specify the column name.
要从表中获取特定列的结果,我们必须指定列名。
Use following code : -
使用以下代码:-
$result = DB::Table('table_name')->select('column1','column2')->where('id',1)->get();
for example -
例如 -
$result = DB::Table('Student')->select('subject','class')->where('id',1)->get();