Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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:

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

Alternatively, you may use the limit and offset methods:

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

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

like image 321
rotaercz Avatar asked Jul 15 '17 16:07

rotaercz


People also ask

What is limit in Laravel?

Limit method will give you the restricted number of rows defined in query. For example if you have 100 rows and you want the 10 rows in query then we need to pass 0, 10 to fetch the first 10 rows. Laravel eloquent or query builder limt method do the same as limit MySQL function.

What is take in Laravel?

take() will help to get data from a database table with a limit. skip() will help to skip some records when you fetch data from the database table. So, let's see bellow examples that will help you how to use take() and skip() eloquent query in laravel. Example: take()

How can I limit collection in Laravel?

Bookmark this question. Show activity on this post. $data->offset(0)->limit(5)->get();


3 Answers

With the Query Builder, take() is just an alias for 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);
}

NB This is not to be confused with take() on Collections.

like image 145
Rwd Avatar answered Oct 20 '22 18:10

Rwd


limit only works for eloquent ORM or query builder objects, whereas take works for both collections and the ORM or Query Builder objects.

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

Model::take(20)->get()    // Correct
Model::limit(20)->get()   // Correct
like image 36
X 47 48 - IR Avatar answered Oct 20 '22 17:10

X 47 48 - IR


if take is used before get, it is the same as limit. If used after get, the action is performed by php itself. that is

Model ::limit(10)->get() = Model ::take(10)->get()

and take to get

Model :: take(10)->get()

Launched through Query/Builder

 public function take ($value)
 {
     return $this->limit($value);
 }}

method to be starts. Sql

select * from `table` where
  `table`.`deleted_at` is null
limit 20

If used after get

  Model :: get()->take(10);

Launched through Collection

 public function take ($limit)
 {
     if ($limit<0) {
         return $this->slice($limit, abs($limit));
     }}

     return $this->slice(0, $limit);
 }}

method worked. and all the data is retrieved via sql and then $limit (10) are allocated via php

Sql

select * from `table` where
  `table`.`deleted_at` is null
like image 1
Jahongir Tursunboyev Avatar answered Oct 20 '22 18:10

Jahongir Tursunboyev