Display first n record from collection in laravel view

Updated at 06-Sep-2021, By samar

Display first n record from collection in laravel view

Through the use of the programming language, we will work together to solve the "Display first n record from collection in laravel view" puzzle in this lesson.

You can display first n record from collection in laravel view using take() method. Pass the number of records you want to display and it will display first n record from colllection
  • Display first 3 records from users collection in laravel view

    @foreach($users->take(3) as $key => $user)
        {{ $user->name }}
    @endforeach
    

    It will display first three records from user collection in laravel view file even if it has more than three records in it. To display data in view file you have to get the users record and return to view file.

    Get the users records and return to view file to display it.

    $users = User::select('*')->paginate(10);

    return view('home')->with(['users'=>$users]); .

Back to code snippet queries related laravel