I am extracting loads of data about 2000000 records. When i am fetching data, i get 500 internal error due to memory size. However, i tried using chunk to get the data in bits of about 1000 each returned to the client.
But in my code below, i am still getting 500 internal error. What am i doing wrong.
I want the data to be returned in bits for the user and not overload the memory
Controller
public function getSpecifiedPeriod($user, $from, $to)
    {
        $connection = DB::connection('database');
        $query = $connection->table('items')->where('user_id', $user)->whereBetween('date_time', [$from, $to])->get()->chunk(100);
        if ($query->isEmpty())
            return false;
        return $query;
    }
