php Laravel 5.2 - pluck() 方法返回数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34405138/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:57:38  来源:igfitidea点击:

Laravel 5.2 - pluck() method returns array

phplaraveleloquentquery-builderlaravel-5.2

提问by Limon Monte

I'm trying to upgrade my project L5.1 -> L5.2. In upgrade guidethere's one thing which isn't clear for me:

我正在尝试升级我的项目 L5.1 -> L5.2。在升级指南中,我不清楚一件事:

The listsmethod on the Collection, query builder and Eloquent query builder objects has been renamed to pluck. The method signature remains the same.

lists对收集,查询生成器和雄辩的查询生成器对象的方法已更名为pluck。方法签名保持不变。

That's ok, rename refactoting from lists()to pluck()isn't a problem. But what with useful pluck()method which was in L5.0 and L5.1?

没关系,重命名重构从lists()topluck()不是问题。但是pluck()L5.0 和 L5.1 中的有用方法呢?

From the 5.0 documentation:

5.0 文档

Retrieving A Single Column From A Row

$name = DB::table('users')->where('name', 'John')->pluck('name');

从一行中检索单列

$name = DB::table('users')->where('name', 'John')->pluck('name');

What is the alternative for old pluck()method in L5.2?

pluck()L5.2 中旧方法的替代方法是什么?

UPDATE:

更新:

Example:

例子:

var_dump(DB::table('users')->where('id', 1)->pluck('id'));

L5.1:

L5.1:

// int(1)

L5.2:

L5.2:

// array(1) { [0]=> int(1) }

回答by user1669496

The current alternative for pluck()is value().

当前的替代方案pluck()value()

回答by NuOne

laravel pluck returns an array

laravel pluck 返回一个数组

if your query is:

如果您的查询是:

 $name = DB::table('users')->where('name', 'John')->pluck('name');

then the array is like this (key is the index of the item. auto incremented value):

然后数组是这样的(键是项目的索引。自动递增的值):

[
    1 => "name1",
    2 => "name2",
    .
    .
    .
    100 => "name100"
]

but if you do like this:

但如果你这样做:

$name = DB::table('users')->where('name', 'John')->pluck('name','id');

then the key is actual index in the database.

那么关键是数据库中的实际索引。

key||value
[
    1 => "name1",
    2 => "name2",
    .
    .
    .
    100 => "name100"
]

you can set any value as key.

您可以将任何值设置为键。

回答by reshma

In Laravel 5.1+, you can use the value() instead of pluck.

在 Laravel 5.1+ 中,您可以使用 value() 代替 pluck。

To get first occurence, You can either use

要获得第一次出现,您可以使用

DB::table('users')->value('name');

or use,

或使用,

DB::table('users')->where('id', 1)->pluck('name')->first();

回答by Jeff Kopmanis

In the original example, why not use the select() method in your database query?

在原始示例中,为什么不在数据库查询中使用 select() 方法?

$name = DB::table('users')->where('name', 'John')->select("id");

This will be faster than using a PHP framework, for it'll utilize the SQL query to do the row selection for you. For ordinary collections, I don't believe this applies, but since you're using a database...

这将比使用 PHP 框架更快,因为它将利用 SQL 查询为您进行行选择。对于普通集合,我认为这不适用,但是由于您使用的是数据库......

Larvel 5.3: Specifying a Select Clause

Larvel 5.3:指定选择子句