Tin*_*tin 1 php laravel eloquent
根据laravel docs,take()只是limit()的别名.
为什么不查询no 4不起作用,而其他工作就好了.
1. $employee->take(2)->get(); // Works
2. $employee->limit(2)->get(); // Works
3. $employee->get()->take(2); // Works
4. $employee->get()->limit(2) // Gives Error; Method Illuminate/Database/Eloquent/Collection::limit does not exist.
Run Code Online (Sandbox Code Playgroud)
当你打电话get,你的数据库查询被执行,所以结果是Illuminate\Support\Collection,当你打电话limit的Collection,因为引发错误Collection类不具有limit的功能.
limit并且take都在你的数据库查询,因为它们是两个函数执行Illuminate\Database\Query\Builder和Illuminate\Database\Eloquent\Builder也,不上Collection.
注意:第三个语句有效,因为Illuminate\Support\Collection还有一个take函数.