我想使用 SQL 查询中的数据,这里是我的代码进一步解释:
$myquery = DB::table('attendances')->select('user_id')->where('date_only', '=', $newdate)->orderBy('logon','asc')->get();
$myquery->user_id;
现在我希望我的 $myquery 的值是 USER_ID 的值,如果我使用上面的代码 -$myquery->user_id; 会出错
最佳答案
编辑:从版本 5.1 pluck
已弃用,因为 5.2 其含义完全不同(如 lists
以前是),所以改用 value
方法。
您正在尝试获取数组的属性,因此显然您遇到了错误。
如果您只需要单个字段,则使用 pluck
(默认返回单个值 - 字符串):
// let's assume user_id is 5
DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->pluck('user_id'); // "5"
当你需要整行时,然后first
(返回stdObject):
$att = DB::table('attendances')
->where('date_only', '=', $newdate)
->orderBy('logon','asc')
->first();
$att->user_id; // "5"
$att->any_other_column;
而 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"
关于php - Laravel如何使用查询生成器返回单列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27143961/