Laravel游标使用困境

时间:2018-01-12 11:29:49

标签: php laravel-5 eloquent

getcursor返回空白

时有效
$excep[] = DB::table('table')->where('user_id', $user)->select('data')->get();
return $excep;  // returns some sql data

但是cursor

$excep[] = DB::table('table')->where('user_id', $user)->select('data')->cursor();
return $excep; // returns [{}]

信息不足以理解:https://laravel.com/docs/5.5/eloquent#chunking-results

1 个答案:

答案 0 :(得分:0)

cursor仅可用于迭代,如果您一次需要所有数据,则必须使用get

// All users will be loaded on memory first on collection and then, iterate.
// You can return all users as return UserModel::get();

foreach (UserModel::get() as $user) {
    $this->sendWellcomeMail($user);
}
// Only one user on memory on each iteration.
// You can NOT return all users with return UserModel::cursor();

foreach (UserModel::cursor() as $user) {
    $this->sendWellcomeMail($user);
}