I have a custom query that grabs data from the old system and maps it to models in the new system. The query looks like this:
$companies = DB::connection('legacy')->select("...");
And since it's a lot of data, I'd like to use Eloquent's chunk feature (just sample code copied from their docs):
User::chunk(200, function($users)
{
foreach ($users as $user)
{
//
}
});
How do I implement this?
Edit: My code now looks like this, which results in no response:
DB::connection('legacy')->select("SELECT * FROM companies")->chunk(200, function($companies) {
foreach ($companies as $company) {
// dd($company);
$entity = Entity::firstOrNew(['external_id' => $company->companyKey]);
$entity->name = $company->companyName;
$entity->save();
}
});