Shady Hesham's avatar

How to fetch big amount of records to be printed

i need to fetch around 4k records which are going to be printed and shown in blade, but when I use get() or all() I get 500 error

0 likes
18 replies
Snapey's avatar

and that error says.... ?

4k is nothing

Shady Hesham's avatar

@Snapey This page isn’t working. is currently unable to handle this request. HTTP ERROR 500.

jlrdw's avatar

What does your route, model, controller, view code look like? Check network tab as well. What does error log say?

Shady Hesham's avatar

@jlrdw i am using simple query which is:

$accounts = Acc_account::with("items")->where('cat', 2)->get(); and I foreach it in a blade but the problem is with items that can be 1000k for each account

Tray2's avatar

And what does the 500 error say? 500 is usually a developer mistake.

jlrdw's avatar

@Tray2 do you mean sometimes the problem is between the keyboard and the chair? Haha 😁

1 like
Shady Hesham's avatar

@Tray2 i agree that sometimes 500 errors cause this problem but in my case If i limit the query with 500 for example it works fine, hence the problem is all about the number of data i am fetching

Tray2's avatar

@Shady Hesham Then you probably run out of memory on the server. I suggest that you either paginate the data, or use ajax to load it increamentally.

Shady Hesham's avatar

@Tray2 thank you for your help, Paginate works fine to surface the data but what about printing?

DhPandya's avatar

@shady Try to implement the Laravel Pagination. It is good when you're dealing with a large amount of Data.

jlrdw's avatar

@DhPandya I agree with pagination for the displaying. As far as printing @shady hesham

  • Do all need printing or just a subset of the records.
  • Many companies these days just browses the data needed via a search
  • If only a smaller subset, you can export to pdf for printing

Just suggestion.

Also most bigger companies uses other software as well. One department for the State of Texas would dump the data to local and via an ODBC connection print employee recall rosters via Microsoft Access. The point is only print what is absolutely necessary to print.

Shady Hesham's avatar

@sylar i have used chunk but i could not print it in blade and here is my code $accounts = Acc_account::where('cat', 2)->chunk(200, function (account) { foreach ($accounts as $account) { // } }); ;

and when I foreach $accounts in blade I get nothing

DhPandya's avatar

@Shady Hesham You have a syntax mistake in the posted answer. Please try with the below code.

Acc_account::where('cat', 2)->chunk(200, function (Collection $accounts) {
            foreach ($accounts as $account) {
                //iterate your accounts here
             }
        });
Snapey's avatar

You can send 4000 rows of data to the browser but it's going to potentially take a long time to render.

It all depends on how complicated the view is and you having enough memory to construct the view.

Normally if I'm asked to dump out this much information I would do it as a CSV or excel and then let the user print from that rather from the html.

Please or to participate in this conversation.