Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Guru5005's avatar

Laravel Chunk

I have data base table with thousands of rows, i am trying to use the chunk method in laravel to return the data to view , but it is returning array as true, can some one help me?

$keys = LanguageMapper::where('lag_id_fk', 2)->where('module_id_fk', $mod_id)
                    ->chunk(250, function ($keys) use($other) {
                        foreach ( $keys as $i => $key )
                        {
                            $finalObj[$i]['key']   = $key['key_name'];
                            $finalObj[$i]['en_text']   = $key['text'];
                            $finalObj[$i]['trans_text']   = isset($other[$key['key_name']]['text']) ? $other[$key['key_name']]['text'] : '';
                            $finalObj[$i]['trans_key_id']   = isset($other[$key['key_name']]['id']) ? $other[$key['key_name']]['id'] : 0;
                            $finalObj[$i]['trans_key_status']   = isset($other[$key['key_name']]['status']) ? $other[$key['key_name']]['status'] : 0;
                        }
                        return $finalObj;
                    });

And i cannot use pagination because i am iterating results. please ignore the arrays as it is not the whole code, i just need to know how i should return the results of foreach.

0 likes
2 replies
Snapey's avatar

Not sure what you think chunk will achieve?

Its purpose is to reduce memory load on the server, which is completely negated if you just sit in a loop concatenating the results?

I don't know what 'returning array as true' possibly means?

Guru5005's avatar

@SNAPEY - I just want to get those thousands of records without pagination. How can I achieve that ? is it possible without pagination? Any help would be appreciated

Please or to participate in this conversation.