查询生成器中的Laravel-4分页

时间:2014-02-14 08:19:29

标签: laravel pagination laravel-4

如何在使用查询构建器时在laravel-4.1中启用分页。

这是我的模特功能。

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

3 个答案:

答案 0 :(得分:4)

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

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

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

答案 1 :(得分:3)

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

答案 2 :(得分:0)

如果您在返回视图之前通过控制器加入模型,请执行:

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

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

并在您的视图中使用{{$pagination}}