php Laravel 如何使用查询生成器返回单列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27143961/
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 how to return single column value using Query Builder
提问by user3349495
I want to use the data from the SQL Query, to explain it further here is my code:
我想使用来自 SQL 查询的数据,进一步解释它,这是我的代码:
$myquery = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();
$myquery->user_id;
Now I want the value of my $myquery to be USER_ID's value, I get error if I used the Above code -$myquery->user_id;
现在我希望 $myquery 的值是 USER_ID 的值,如果我使用上面的代码 -$myquery->user_id;
回答by Jarek Tkaczyk
EDIT: As of version 5.1pluck
is deprecated, since 5.2its meaning is completely different (as lists
used to be), so use value
method instead.
编辑:不推荐使用5.1版pluck
,因为5.2 版它的含义完全不同(和lists
以前一样),所以改用value
方法。
You're trying to get property of the array, so obviously you're getting error.
您正在尝试获取数组的属性,因此显然您遇到了错误。
If you need single field only, then use pluck
(which returns single value - string by default):
如果您只需要单个字段,则使用pluck
(默认返回单个值 - 字符串):
// let's assume user_id is 5
DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->pluck('user_id'); // "5"
When you need whole row, then first
(which returns stdObject):
当您需要整行时,则first
(返回 stdObject):
$att = DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->first();
$att->user_id; // "5"
$att->any_other_column;
And get
is when you want multiple results (which returns simple array of stdObjects):
并且get
是当您想要多个结果(返回简单的 stdObjects 数组)时:
$result = DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->get();
$result; // array(0 => stdObject {..}, 1 => stdObject {..}, ...)
$result[0]->user_id; // "5"
回答by Слаффка Островский
That's for Laravel 5.3:
这是 Laravel 5.3:
$user_id = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->value('user_id');
回答by Vinil Vinayan
This will return user_id only
这将仅返回 user_id
$myquery = DB::table('attendances')->where('date_only', $newdate)->select('user_id')->pluck('user_id')->first();
回答by S Debasish Nayak
To get only the list of value of a single column you can do like this.
要仅获取单个列的值列表,您可以这样做。
$result = CustomerInfo::select('eventDate')->pluck('eventDate');
here $result will give you the result like this:
这里 $result 会给你这样的结果:
["2019-03-25","2020-03-31","2019-03-31","2019-04-24","2019-11-26"]
hope it may help someone
希望它可以帮助某人
回答by 17K
According to the latest documentation: 5.6
根据最新文档:5.6
DB::table('users')->select('name');
will do the trick.
会做的伎俩。
回答by Ankit Vishwakarma
I think there is some confusion; but I am using Laravel 5.3 and pluck and value function is not deprecated as depicted above.
我认为有些混乱;但我使用的是 Laravel 5.3 并且 pluck 和 value 函数没有被弃用,如上所示。
For more clarification: pluck()will be used instead of get()when you want all the values only value()will be used instead of first()when you want only one value.
更多说明: 当您想要所有值时,将使用pluck()而不是get()当您只想要一个值时,仅使用 value()而不是first()。
use method all()to get the array of itmes. usage:
使用方法all()获取项目数组。用法:
\App\User::whereIn('mobile',['971700000', '965000000'])->select('id')->pluck('id')->all();
回答by NIDHINgL
DB::table('table_name')->select('column_na')->get();
DB::table('table_name')->select('column_na')->get();
回答by Lonac Lonac
If you have data in array already and you want to filter only such kind of data, you can do this as well
如果您已经在数组中有数据并且只想过滤此类数据,您也可以这样做
0 => "first_name"
1 => "middle_name"
]
$users_data = User::where('id', '1')->get($filter_data );
return $users_data; ```
<pre> In laravel 5.4 works perfect for me </pre>
回答by itachi
$myquery = DB::table('attendances')->select('user_id')
->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();
by default, the returned data will be array of objects. you need to loop over it.
默认情况下,返回的数据将是对象数组。你需要遍历它。
e.g.
例如
foreach($myquery as $i)
{
echo $i->user_id;
}