编辑:截至版本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;
And 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"