Call to a member function paginate() on array

Erfan Ahmed picture Erfan Ahmed · Apr 2, 2016 · Viewed 18.6k times · Source

I am working with Laravel5.0 . I want to add paginate() to the following function of my controller part.

public function index()
{
    try{
        $val=DB::connection()->getDatabaseName();
        if(DB::connection()->getDatabaseName()) {
            //$bRecord = DB::table('bills')->orderBy('Date', 'desc')->paginate(4);

            $bRecord = DB::table('bills')
                ->join('clients', 'bills.ClientID', '=', 'clients.ClientID')
                ->select('bills.ReceiptID', 'bills.ClientID', 'bills.Paid', 'bills.Date','bills.ReceivedBy',
                    'clients.ClientName')
                ->get();

            return view('bills.billRecord')->with('bRecord', $bRecord);
        }else{
            $er="/connection status: database error";
            return view('home')->with('error',$er);         //'error' is passed to home
        }
    }catch (\Exception $e){
        $er="/connection status: database error";
        return view('home')->with('error',$er);
    }

}

if I add paginate here, it shows error. "Call to a member function paginate() on array"

$bRecord = DB::table('bills')
                ->join('clients', 'bills.ClientID', '=', 'clients.ClientID')
                ->select('bills.ReceiptID', 'bills.ClientID', 'bills.Paid', 'bills.Date','bills.ReceivedBy',
                    'clients.ClientName')
                ->get()->paginate(4);

How can I use paginate() here?

Answer

KazikM picture KazikM · Apr 2, 2016

Example from Laravel 5.0 Pagination documentation:

$users = DB::table('users')->paginate(15);

So try to change last line of your example from

 ->get()->paginate(4);

to

->paginate(4);