laravel 首先使用查询构建器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26054354/
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
Pluck together with first using Query builder
提问by Marcin Nabia?ek
If I want to get get name of first user in database using eloquent I can do something like that:
如果我想使用 eloquent 获取数据库中第一个用户的名称,我可以执行以下操作:
$user = User::select('name')->first()->pluck('name');
// or $user = User::first()->pluck('name');
echo $user;
to get only name of this user as string.
仅以字符串形式获取此用户的名称。
However If I try the same using only query builder:
但是,如果我仅使用查询构建器尝试相同的操作:
$user = DB::table('users')->select('name')->first()->pluck('name');
echo $user;
I get exception:
我得到例外:
Call to undefined method stdClass::pluck()
调用未定义的方法 stdClass::pluck()
But without using first it will work:
但是不先使用它会起作用:
$user = DB::table('users')->select('name')->where('id',1)->pluck('name');
echo $user;
Is it not possible to use pluck
with first
using query builder or am I doing something wrong?
这难道不是可以使用pluck
与first
使用查询生成器或我做错了什么?
PS. Of course I know that I can display any property using $user->name
without using pluck
but I'm just curious why using Eloquent it works and using Query Builder it works only when not having both first
and pluck
附注。当然,我知道我可以在$user->name
不使用的情况下显示任何属性,pluck
但我只是好奇为什么使用 Eloquent 它可以工作,而使用 Query Builder 它仅在没有同时使用first
和pluck
回答by Jarek Tkaczyk
You don't want to use pluck
with first
, because it's redundant:
您不想使用pluck
with first
,因为它是多余的:
$query->first() // fetch first row
->pluck('name'); // fetch first row again and return only name
So use only pluck
:
所以只使用pluck
:
$query->pluck('name');
It does all you need.
它可以满足您的所有需求。
But there's more.
但还有更多。
Under the hood first
and pluck
run 2 separate queries, so:
下罩first
和pluck
运行2个独立的查询,因此:
$query->where(..)->orderBy(..)->first() // apply where and orderBy
->pluck('name'); // no where and orderBy applied here!
So it's not only redundant to use first
before pluck
, but also it causes unexpected results.
所以使用first
before不仅是多余的pluck
,而且还会导致意想不到的结果。
You can chain those methods on Eloquent query, since it returns a collection (get
) or model (first
), but Query\Builder
returns just an array (get
) or stdObject
(first
). That's why you couldn't do the same oon the query builder.
你可以在 Eloquent 查询上链接这些方法,因为它返回一个集合 ( get
) 或模型 ( first
),但Query\Builder
只返回一个数组 ( get
) 或stdObject
( first
)。这就是为什么你不能在查询构建器上做同样的事情。
回答by Joren
In Laravel 5.1+, you can use the value
method.
在 Laravel 5.1+ 中,您可以使用该value
方法。
From the docs:
从文档:
If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
如果您甚至不需要整行,您可以使用 value 方法从记录中提取单个值。该方法将直接返回列的值:
$email = DB::table('users')->where('name', 'John')->value('email');
This is how it works behind the scenes in the Builder class:
这是它在 Builder 类中的幕后工作方式:
/**
* Get a single column's value from the first result of a query.
*
* @param string $column
* @return mixed
*/
public function value($column)
{
$result = (array) $this->first([$column]);
return count($result) > 0 ? reset($result) : null;
}
回答by reshma
Current alternative for pluck()is value().
当前pluck() 的替代方法是value()。
To get first occurence, You can either use
要获得第一次出现,您可以使用
DB::table('users')->value('name');
or use,
或使用,
DB::table('users')->pluck('name')->first();
回答by hamidreza samsami
Try this one:
试试这个:
$user = User::pluck('name')->first();
echo $user;