Faced the error when wanted to create pagination in Laravel

laravel pagination
laravel pagination total count
laravel custom pagination
laravel manual pagination
laravel pagination limit
laravel pagination with search
laravel bootstrap pagination
laravel pagination offset

I wanted to create pagination in index page and as I read in Laravel documentation it is possible to use paginate method with orderBy method when I do that I faced error:"Method Illuminate\Database\Eloquent\Collection::orderBy does not exist.". The code I am using:

public function index()
    {
        $user_id=auth()->user()->id;
        $user=User::find($user_id);
        $personal=$user->personalAccounting->orderBy('asc')->paginate(2);
        $balance=$user->balance;
        return view('personal.index', compact('personal','balance'));

    }  


 @extends('layouts.app')
    @section('content')
    <h1>Домашняя Бухгалтерия</h1>
    <br>
    @if($balance==0)
    <h2 class="text-center">Ваш Баланс: <span class="badge badge-primary">0.00</span></h2>
    @elseif($balance<0)
    <h3 class="text-center">Ваш Баланс: <span class="badge badge-danger">{{$balance}}</span></h3>
    @else
    <h3 class="text-center">Ваш Баланс: <span class="badge badge-success">+{{$balance}}</span></h3>
    @endif
    <br>
    <a href="/personal/create" class="btn btn-primary">Создать</a>
    <br>
    @if(count($personal)>0)
    @foreach($personal as $pers)
    <br>
        <div class="card">
            <h2><a href="/personal/{{$pers->id}}" >{{$pers->TypeOfAccounting}}</a></h2>
        <h5 class="">Наименование:<span class="badge">{{$pers->Name}}</span></h5>
        </div>
    @endforeach
    {{$personal->links()}}
    @else
    <p>Не найдено записей</p>


    @endif

    @stop
public function index()
    {
        $user = auth()->user(); // It's already the user model

        $personal = $user->personalAccounting()->orderBy('YOUR_COLUMN')->paginate(2);
        $balance = $user->balance;
        return view('personal.index', compact('personal','balance'));

    }  

Notice personalAccounting() instead of personalAccounting The reason is, when you call the attribute without (), Laravel will load all the data from your DB in a collection. Every modification you do after, will be done with PHP. When you request with the method using (), you're modifying the QueryBuilder which mean the orderBy and the pagination will be executed by MySQL which will also increase the performance and reduce the memory usage.

php, I wanted to create pagination in index page and as I read in Laravel documentation it is possible to use paginate method with orderBy method  Finally a solution to a simple pagination, not this new "create 5 php files, implement a class and write 200 lines of code". FINALLY! Thank you. – hfingler Mar 25 '15 at 3:03

Give a column name before asc

public function index()
    {
        $user_id=auth()->user()->id;
        $user=User::find($user_id);
        $personal=$user->personalAccounting->orderBy('**your_column_name**','asc')->paginate(2);
        $balance=$user->balance;
        return view('personal.index', compact('personal','balance'));

} 

Faced the error when wanted to create pagination in Laravel , I wanted to create pagination in index page and as I read in Laravel documentation it is possible to use paginate method with orderBy method  We are Recommending you: Laravel 5.7 - Generate PDF from HTML Example; Laravel 5.7 - Create REST API with authentication using Passport Tutorial

Try to use latest().

public function index()
    {
        $user_id=auth()->user()->id;
        $user=User::find($user_id);
        $personal=$user->personalAccounting->latest('***Add column name***')->paginate(2);
        $balance=$user->balance;
        return view('personal.index', compact('personal','balance'));

    }  

Laravel Manual Pagination - Nelly Tadi, This tutorial explains how to manually create a paginator in Laravel. To have a better Recently, I was faced with a problem of paginating an array of data. My data was The results returned were merged and sorted in one array and I needed to Paginate it before returning to my view. Error message on paginate()​. Laravel pagination for custom queries Published on July 15, 2015 in Laravel If you have used laravel for any CRUD application you may used the paginate method for generating pagination links.

[5.6.27] Pagination pages count incorrect · Issue #25804 · laravel , Laravel Version: 5.6.27 PHP Version: 7.1.11 Database Driver & Version: MariaDB 10 Description: You want to do $collection->total() to get the total number of records I have faced same issue having next configuration:. When creating a pagination in PHP, you most often want to display pages. Displaying pages. Would you want to display pages you need another presenter, the Illuminate\Pagination\BootstrapThreePresenter. You can’t use this presenter with the default Paginator. Instead you need to create a Illuminate\Pagination\LengthAwarePaginator. This paginator accepts one more parameter when instantiated, the rest is the same.

Paged Resource Collection not working, an API and I want to use Laravel's API Resources, but I'm facing a bit of to wrap your data in an array to actually make the pagination work. Simple Pagination. Laravel gives you ability to create "Simple Pagination", which will have only Previous and Next buttons, something like Bootstrap has. The landish/pagination package supports this kind of pagination for Semantic UI, Zurb Foundation and UIKit. In order to use, first call the simplePaginate() method on Eloquent Model.

Laravel 5.3 pagination problem with last page, Currently, I'm working on a Laravel 5.3 project. I'm facing a problem with pagination on the last page. If the last page has more than one item,  Laravel is a web application framework with expressive, elegant syntax. We believe development must be an enjoyable and creative experience to be truly fulfilling. Laravel attempts to take the pain out of development by easing common tasks used in the majority of web projects, such as:

Comments
  • hey dude thanks for your cool solution and with explanation.:)
  • hi Minar, I have put column name but received exactly the same error:"Method Illuminate\Database\Eloquent\Collection::orderBy does not exist."
  • @MayuriPansuriya, hi Mayuri, yeah tried it but got Undefined property: Illuminate\Database\Eloquent\Builder::$personalAccounting"
  • @MayuriPansuriya, the code I am using $user_id=auth()->user()->id; $user=User::where('id', $user_id); $personal=$user->personalAccounting->orderBy('id','asc')->paginate(2); $balance=$user->balance; return view('personal.index', compact('personal','balance'));
  • @MayuriPansuriya, I have updated my question, please check it out, and sorry I am from Russia thus the app is inn Russian:)