在 Laravel Eloquent 中,limit 与 take 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45120135/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:18:14  来源:igfitidea点击:

In Laravel Eloquent what is the difference between limit vs take?

phplaraveleloquent

提问by rotaercz

In the documentation it shows the following:

在文档中,它显示了以下内容:

To limit the number of results returned from the query, or to skip a given number of results in the query, you may use the skip and take methods:

要限制查询返回的结果数量,或跳过查询中给定数量的结果,您可以使用 skip 和 take 方法:

$users = DB::table('users')->skip(10)->take(5)->get();

Alternatively, you may use the limit and offset methods:

或者,您可以使用 limit 和 offset 方法:

$users = DB::table('users')
            ->offset(10)
            ->limit(5)
            ->get();

What are the differences between these two? Are there any differences in execution speed?

这两者之间有什么区别?执行速度有区别吗?

回答by Rwd

take()is just an alias for limit():

take()只是一个别名limit()

/**
 * Alias to set the "limit" value of the query.
 *
 * @param  int  $value
 * @return \Illuminate\Database\Query\Builder|static
 */
public function take($value)
{
    return $this->limit($value);
}

Hope this helps!

希望这可以帮助!

回答by X 47 48 - IR

limitonly works on eloquent ORM or query builder objects whereas takeworks on both collections and the ORM or query builder objects.

limit仅适用于 eloquent ORM 或查询构建器对象,而take适用于集合和 ORM 或查询构建器对象。

Model::get()->take(20);   // Correct
Model::get()->limit(20);  // Incorrect

Model::take(20)->get()    // Correct
Model::limit(20)->get()   // Correct