Laravel Raw查询分页;对eloquent对象的原始查询

时间:2014-04-06 20:08:43

标签: php mysql database laravel-4 eloquent

select '0000-00-00' as date, 'Opening Balance' as narration, (SELECT debit FROM `account_sub_journal` where id=1)as debit, (SELECT credit FROM `account_sub_journal` where id=1)as credit, '0' as transaction_entry_id,'0' as account_sub_journal_id  UNION SELECT * FROM `ledgertransactions` where account_sub_journal_id = 1 and `date` BETWEEN  '2014-04-01' and '2014-04-10'

我目前在模型中将其作为静态函数。由于laravel说它不是一个对象,我无法对此进行分页

public static function ledgerbook_to($account_id,$date){
        $book =  DB::select( DB::raw("SELECT  '0000-00-00' AS DATE,  'Opening Balance' AS narration, (SELECT debit FROM  `account_sub_journal` WHERE id =1) AS debit, (SELECT credit FROM  `account_sub_journal` WHERE id = :account_id) AS credit,  '0' AS transaction_entry_id,  '0' AS account_sub_journal_id UNION SELECT * FROM  `ledgertransactions` WHERE account_sub_journal_id =:account_id_t and `date` <= :date_to "), array(
   'account_id' => $account_id,'account_id_t' => $account_id,'date_to' => $date));
        return $book;
    }

如果至少在eloquent中解决以下问题,我可以结合这个。

SELECT  '0000-00-00' AS DATE,  'Opening Balance' AS narration, (SELECT debit FROM  `account_sub_journal` WHERE id =1) AS debit, (SELECT credit FROM  `account_sub_journal` WHERE id =1) AS credit,  '0' AS transaction_entry_id,  '0' AS account_sub_journal_id

感谢您的支持

1 个答案:

答案 0 :(得分:3)

您可以使用以下内容手动生成分页链接:

$pagination = Paginator::make($book, count($book), 5);

然后你可以使用这样的东西:

echo $pagination->links();

或(Blade)这个:

{{ $pagination->links() }}

检查the documentation以了解有关手动创建分页的详情。