Get only 10 records from table in laravel

Created at 29-Aug-2021 , By samar

Get only 10 records from table in laravel

In this session, we will try our hand at solving the "Get only 10 records from table in laravel".

You can get 10 records from table in laravel. There are many methods available in laravel to get the specific number of records like take(10), get(10). The method take() fetch the number of rows from table by specifying the value to it.
  • Get only 10 records in laravel using controller's method

    //routes\web.php
    Route::get('/get-10-records', [HomeController::class, 'getRecords']);
    
    //Create controller if not exists using
    php artisan make:controller HomeController
    
    //app\Http\Controllers\HomeController.php
    use App\Models\User;
    
    class HomeController extends Controller
    {
        public function getRecords(){
            $users = User::select('*')->take(10)->get();
            return response()->json($users);
        }
    }
    

    This code snippet helps you to get the specific number of records from table in laravel.

  • Get only 10 records in laravel using web.php

    --PATH routes\web.php
    Route::get('/get-10-records', function(){
        $users = App\Models\User::select('*')->take(10)->get();
        return response()->json($users);
    });
    

    Copy/Paste code in web.php file and run the php artisan serve command and it will display the output

    http://laravel.test/get-10-records

Back to code snippet queries related laravel

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Buy Me A Coffee

Don't forget to share this article! Help us spread the word by clicking the share button below.

We appreciate your support and are committed to providing you valuable and informative content.

We are thankful for your never ending support.