How to use skip() and take() in Laravel Query?


Share:
Published By: Admin, Published On: May 04, 2021, Category: Laravel

Hello Friends,

Now let's see example of how to use take() and skip() in laravel. This is a short guide on laravel if take(), skip(). We will use how to use take() and skip() in laravel. Here you will learn how to use take() and skip() in laravel. Let's get started with how to take() and skip() use in laravel.

Here i will give you many example and understand how works this take and skip functions one by one in laravel.

Example take():

The take method returns a new collection with the specified number of items.

/**
 * Write code on Method
 *
 * @return response()
*/
public function index()
{
    $logos = Logo::select("*")
                    ->take(2)
                    ->get()
                    ->toArray();

    dd($logos);
}
Output:
array:2 [▼
  0 => array:5 [▼
    "id" => 1
    "logo" => "1617281116-creative-market.jpg"
    "link" => "creative.com"
    "created_at" => "2021-04-01T12:45:16.000000Z"
    "updated_at" => "2021-04-01T12:45:16.000000Z"
  ]
  1 => array:5 [▶]
]
Example skip():

The skip method returns a new collection, with the given number of elements removed from the beginning of the collection.

/**
 * Write code on Method
 *
 * @return response()
*/
public function index()
{
    $logos = Logo::select("*")
                    ->skip('1')
                    ->offset(0)
                    ->limit(5)
                    ->get()
                    ->toArray();
    dd($logos);
}
Output:
array:4 [▼
  0 => array:5 [▼
    "id" => 1
    "logo" => "1617281116-creative-market.jpg"
    "link" => "creative.com"
    "created_at" => "2021-04-01T12:45:16.000000Z"
    "updated_at" => "2021-04-01T12:45:16.000000Z"
  ]
  1 => array:5 [▶]
  2 => array:5 [▶]
  3 => array:5 [▶]
]

It will help you....

Share: