Laravel-4 Pagination in Query Builder

How to enable pagination in laravel-4.1 when using the query builder.

Here is my function from the model.

public static function getTransactions( $accountCode ) {
        $accountId = Account::getAccountId( $accountCode );
        $object = DB::table('transactions')
            ->orderBy('transactionId', 'ASC')
            ->where('fkAccountId', $accountId)
            ->where('fkUserId', Auth::user()->userId);
            ->get();

        return $object;
    }
+4
source share
3 answers
public static function getTransactions( $accountCode ) {
        $accountId = Account::getAccountId( $accountCode );
        $object = DB::table('transactions')
            ->orderBy('transactionId', 'ASC')
            ->where('fkAccountId', $accountId)
            ->where('fkUserId', Auth::user()->userId);
            ->paginate(10);

        return $object;
    }

and

echo $object->links(); //links

http://laravel.com/docs/pagination#usage

+4
source
   public static function getTransactions( $accountCode ) {
        $accountId = Account::getAccountId( $accountCode );
        $object = DB::table('transactions')
            ->orderBy('transactionId', 'ASC')
            ->where('fkAccountId', $accountId)
            ->where('fkUserId', Auth::user()->userId);
            ->paginate(NUMBER OF PAGES); // whatever you needs this to be

        return $object;
    }
+3
source

if you join your model through your controller before returning the view do:

abstract class YourController extends \BaseController {
    public function index() {
        $modelManager = new yourModel();
        $pagination = $modelManager->getTransactions($accountCode)->links();

        return View::make('yourView', array('pagination' => $pagination));
    }
}

and use {{$pagination}}in your view.

0
source

Source: https://habr.com/ru/post/1526895/


All Articles