Laravel Raw query paginate; raw query to eloquent object

橙三吉。 提交于 2020-01-06 13:11:09

问题


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'

I currently do this as a static function in model. I couldn't paginate this since laravel says it is not an object

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

I could union this if at least solve below query in 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

Thank you for your support


回答1:


You can generate pagination links manually using something like this:

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

Then you may use something like this:

echo $pagination->links();

Or (Blade) this:

{{ $pagination->links() }}

Check the documentation to know more about creating pagination manually.



来源:https://stackoverflow.com/questions/22899354/laravel-raw-query-paginate-raw-query-to-eloquent-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!