Laravel:从MySQL查询中获取一个值。

时间:2022-10-25 08:24:09

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值是一个值= Admin的字符串

but what i get is : [{"groupName":"Admin"}]

但是我得到的是:[{"groupName":"Admin"}]

5 个解决方案

#1


27  

Edit:

编辑:

Sorry i forgot about pluck() as many have commented : Easiest way is :

对不起,我忘记了勇气(),正如许多人所说:最简单的方法是:

return DB::table('users')->where('username', $username)->pluck('groupName');

返回DB:表(“用户”)- >(用户名,用户名美元)- >拔(“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

使用fluent查询构建器,您将获得一个数组。我的意思是,查询构建器不知道该查询将返回多少行。这是你可以做的,让它更干净一点

$result = DB::table('users')->select('groupName')->where('username', $username)->first();

$ = DB:结果:表(“用户”)- >选择(“groupName”)- >(用户名,用户名美元)- >();

The first() tells the queryBuilder to return only one row so no array, so you can do :

第一个()告诉queryBuilder只返回一行,因此不返回数组,因此可以:

return $result->groupName;

返回结果- > groupName美元;

Hope it helps

希望它能帮助

#2


34  

yet another edit: As of version 5.2 pluck is not deprecated anymore, it just got new behaviour (same as lists previously - see sidenote below):

另一项编辑:由于5.2折取不再被弃用,它只是获得了新的行为(与前面的列表相同——参见下面的sidenote):

edit: As of version 5.1 pluck is deprecated, so start using value instead:

编辑:由于5.1折取已被弃用,所以开始使用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没有遵循明智的版本控制,所以有时会出现这样的情况。在编写这个答案的时候,我们有了从查询中获取单个值的方法(Laravel 4)。* & 5.0)。

Then, with L5.1 pluck got deprecated and, instead, we got value method to replace it.

然后,使用L5.1折取被弃用,取而代之,我们使用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 colleciton and query, taht's the reason).

但有趣的是,勇气实际上从未消失。相反,它只是有了全新的行为,而且……弃用列表的方法。(L5.2) -这是由于查询生成器和收集方法之间的不一致引起的(在5.1中,对colleciton和Query的处理不同,taht是原因)。

#3


6  

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表创建一个模型用户,这是与Laravel中的数据库表交互的最有效的方式。

#4


5  

[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

【编辑】调用函数的预期输出已从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中,从查询的第一个结果中提取单个列的值。

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中,用给定列的值提取一个数组。所以它不再被弃用,但也不再像以前那样了。

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.

简而言之,如果你想从第一行中选一列,而你使用的是Laravel 5.1或以上版本,那么可以使用value函数。

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 deprecated and will be removed completely in Laravel 5.2.

对于任何遇到使用Laravel 5.1的问题的人来说,胆量()已经被弃用,将在Laravel 5.2中被完全删除。

Consider future proofing your code by using value() instead.

考虑使用value()来验证您的代码。

return DB::table('users')->where('username', $username)->value('groupName');

#5


2  

Using query builder, get the single column value such as groupName

使用query builder获取单个列值,如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;

#1


27  

Edit:

编辑:

Sorry i forgot about pluck() as many have commented : Easiest way is :

对不起,我忘记了勇气(),正如许多人所说:最简单的方法是:

return DB::table('users')->where('username', $username)->pluck('groupName');

返回DB:表(“用户”)- >(用户名,用户名美元)- >拔(“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

使用fluent查询构建器,您将获得一个数组。我的意思是,查询构建器不知道该查询将返回多少行。这是你可以做的,让它更干净一点

$result = DB::table('users')->select('groupName')->where('username', $username)->first();

$ = DB:结果:表(“用户”)- >选择(“groupName”)- >(用户名,用户名美元)- >();

The first() tells the queryBuilder to return only one row so no array, so you can do :

第一个()告诉queryBuilder只返回一行,因此不返回数组,因此可以:

return $result->groupName;

返回结果- > groupName美元;

Hope it helps

希望它能帮助

#2


34  

yet another edit: As of version 5.2 pluck is not deprecated anymore, it just got new behaviour (same as lists previously - see sidenote below):

另一项编辑:由于5.2折取不再被弃用,它只是获得了新的行为(与前面的列表相同——参见下面的sidenote):

edit: As of version 5.1 pluck is deprecated, so start using value instead:

编辑:由于5.1折取已被弃用,所以开始使用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没有遵循明智的版本控制,所以有时会出现这样的情况。在编写这个答案的时候,我们有了从查询中获取单个值的方法(Laravel 4)。* & 5.0)。

Then, with L5.1 pluck got deprecated and, instead, we got value method to replace it.

然后,使用L5.1折取被弃用,取而代之,我们使用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 colleciton and query, taht's the reason).

但有趣的是,勇气实际上从未消失。相反,它只是有了全新的行为,而且……弃用列表的方法。(L5.2) -这是由于查询生成器和收集方法之间的不一致引起的(在5.1中,对colleciton和Query的处理不同,taht是原因)。

#3


6  

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表创建一个模型用户,这是与Laravel中的数据库表交互的最有效的方式。

#4


5  

[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

【编辑】调用函数的预期输出已从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中,从查询的第一个结果中提取单个列的值。

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中,用给定列的值提取一个数组。所以它不再被弃用,但也不再像以前那样了。

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.

简而言之,如果你想从第一行中选一列,而你使用的是Laravel 5.1或以上版本,那么可以使用value函数。

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 deprecated and will be removed completely in Laravel 5.2.

对于任何遇到使用Laravel 5.1的问题的人来说,胆量()已经被弃用,将在Laravel 5.2中被完全删除。

Consider future proofing your code by using value() instead.

考虑使用value()来验证您的代码。

return DB::table('users')->where('username', $username)->value('groupName');

#5


2  

Using query builder, get the single column value such as groupName

使用query builder获取单个列值,如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;