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.

Answers 2
  • 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);
        }
    }
    
    0

    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);
    });
    
    0

    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