php Laravel:从 MySQL 查询中获取单个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25930841/
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
Laravel: getting a a single value from a MySQL query
提问by Osama Al-Banna
I'm trying get a single value from MySQL database using laravel but the problem I'm getting an array . this is my query result in MySQL command line:
我正在尝试使用 laravel 从 MySQL 数据库中获取单个值,但问题是我得到了一个数组。这是我在 MySQL 命令行中的查询结果:
select groupName from users;
+-----------+
| groupName |
+-----------+
| Admin |
+-----------+
my laravel function:
我的 Laravel 功能:
public static function PermitAddNewUser(){
$username=Session::get('key');
$data=DB::select("select groupName from users where username='$username';");
return $data;
}
expected $data value is a string with value= Admin
预期的 $data 值是一个 value= Admin 的字符串
but what i get is : [{"groupName":"Admin"}]
但我得到的是: [{"groupName":"Admin"}]
回答by Jarek Tkaczyk
yet another edit: As of version 5.2pluck
is notdeprecated anymore, it just got new behaviour (same as lists
previously - see side-note below):
另一个编辑:的版本5.2pluck
是不是不再过时,它刚刚得到新的行为(如同lists
以前-见下文侧面说明):
edit: As of version 5.1pluck
is deprecated, so start using value
instead:
编辑:不推荐使用5.1版pluck
,因此请开始使用value
:
DB::table('users')->where('username', $username)->value('groupName');
// valid for L4 / L5.0 only
DB::table('users')->where('username', $username)->pluck('groupName');
this will return single value of groupName
field of the first row found.
这将返回groupName
找到的第一行字段的单个值。
SIDE NOTE reg. @TomasButeler comment: As Laravel doesn't follow sensible versioning, there are sometimes cases like this. At the time of writing this answer we had pluck
method to get SINGLE value from the query (Laravel 4.* & 5.0).
边注注册。@TomasButeler 评论:由于 Laravel 不遵循合理的版本控制,因此有时会出现这样的情况。在撰写此答案时,我们有pluck
从查询中获取 SINGLE 值的方法(Laravel 4.* 和 5.0)。
Then, with L5.1 pluck
got deprecated and, instead, we got value
method to replace it.
然后,L5.1pluck
被弃用,取而代之的是,我们找到value
了替换它的方法。
But to make it funny, pluck
in fact was never gone. Instead it just got completely new behaviour and... deprecated lists
method.. (L5.2) - that was caused by the inconsistency between Query Builder and Collection methods (in 5.1 pluck
worked differently on the collection and query, that's the reason).
但为了让它有趣,pluck
实际上从未消失。相反,它只是获得了全新的行为和...弃用的lists
方法.. (L5.2) - 这是由查询生成器和集合方法之间的不一致引起的(在 5.1 中pluck
,集合和查询的工作方式不同,这就是原因)。
回答by elfif
Edit:
编辑:
Sorry i forgot about pluck()
as many have commented :
Easiest way is :
抱歉,我忘记了pluck()
许多评论:最简单的方法是:
return DB::table('users')->where('username', $username)->pluck('groupName');
Which will directly return the only the first result for the requested row as a string.
这将直接返回请求行的唯一第一个结果作为字符串。
Using the fluent query builder you will obtain an array anyway. I mean The Query Builder has no idea how many rows will come back from that query. Here is what you can do to do it a bit cleaner
使用流畅的查询构建器,您无论如何都会获得一个数组。我的意思是查询生成器不知道从该查询返回多少行。这是你可以做的事情,让它更干净一点
$result = DB::table('users')->select('groupName')->where('username', $username)->first();
The first()
tells the queryBuilder to return only one row so no array, so you can do :
该first()
通知的QueryBuilder返回只有一行所以没有数组,所以你可以这样做:
return $result->groupName;
Hope it helps
希望能帮助到你
回答by Learner
As of Laravel >= 5.3, best way is to use value:
从 Laravel >= 5.3 开始,最好的方法是使用value:
$groupName = \App\User::where('username',$username)->value('groupName');
or
或者
use App\User;//at top of controller
$groupName = User::where('username',$username)->value('groupName');//inside controller function
Of course you have to create a model User for users table which is most efficient way to interact with database tables in Laravel.
当然,您必须为 users 表创建一个模型 User,这是与 Laravel 中的数据库表进行交互的最有效方式。
回答by Maxime Rainville
[EDIT]The expected output of the pluck
function has changed from Laravel 5.1 to 5.2. Hence why it is marked as deprecated in 5.1
[编辑]该pluck
函数的预期输出已从 Laravel 5.1 更改为 5.2。因此为什么它在 5.1 中被标记为已弃用
In Laravel 5.1, pluck
gets a single column's value from the first result of a query.
在 Laravel 5.1 中,pluck
从查询的第一个结果中获取单个列的值。
In Laravel 5.2, pluck
gets an array with the values of a given column. So it's no longer deprecated, but it no longer do what it used to.
在 Laravel 5.2 中,pluck
获取包含给定列值的数组。所以它不再被弃用,但它不再像以前那样做。
So short answer is use the value
function if you want one column from the first row and you are using Laravel 5.1 or above.
所以简短的回答是,value
如果您想要第一行中的一列并且您使用的是 Laravel 5.1 或更高版本,请使用该函数。
Thanks to Tomas Buteler for pointing this out in the comments.
感谢 Tomas Buteler 在评论中指出这一点。
[ORIGINAL]For anyone coming across this question who is using Laravel 5.1, pluck() has been deprecatedand will be removed completely in Laravel 5.2.
[原文]对于使用 Laravel 5.1 遇到此问题的任何人,pluck () 已被弃用,并将在 Laravel 5.2 中完全删除。
Consider future proofing your code by using value()
instead.
考虑使用value()
代替来验证您的代码的未来。
return DB::table('users')->where('username', $username)->value('groupName');
回答by Majbah Habib
Using query builder, get the single column value such as groupName
使用查询构建器,获取单列值,例如groupName
$groupName = DB::table('users')->where('username', $username)->pluck('groupName');
For Laravel 5.1
对于 Laravel 5.1
$groupName=DB::table('users')->where('username', $username)->value('groupName');
Or, Using user model, get the single column value
或者,使用用户模型,获取单列值
$groupName = User::where('username', $username)->pluck('groupName');
Or, get the first row and then split for getting single column value
或者,获取第一行然后拆分以获取单列值
$data = User::where('username', $username)->first();
if($data)
$groupName=$data->groupName;
回答by MD. Shafayatul Haque
On laravel 5.6 it has a very simple solution:
在 laravel 5.6 上,它有一个非常简单的解决方案:
User::where('username', $username)->first()->groupName;
It will return groupNameas a string.
它将以字符串形式返回groupName。