-
-
Save thinkstylestudio/b2b983f728ec2d156e85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MigrateOrdersAndProducts extends Seeder | |
{ | |
protected $users; | |
protected $orders; | |
public function __construct() { | |
$this->users = []; | |
$this->orders = []; | |
} | |
public function run() { | |
$userKey = User::count()+1; // Store the current id to build up an insert array | |
$lastLegacyUser = LegacyUser::orderBy('legacy_id', 'desc')->first(); | |
$lastLegacyUserId = ($lastLegacyUser) ? $lastLegacyUser->legacy_id : 0; | |
$orderKey = Order::count()+1; | |
$legacyUsers = LegacyUser::where('legacy_id', '>', $lastLegacyUserId)->orderBy('legacy_user_id', 'asc')->take(1000)->get(); | |
foreach($legacyUsers as $legacyUser) { | |
$this->users[] = [ | |
'id' => $userKey, | |
'name' => $legacyUser->name, | |
'legacy_id' => $legacyUser->id, | |
... | |
]; | |
foreach($legacyUser->legacyOrders as $legacyOrder) { | |
$this->orders[] = [ | |
'id' => $orderKey, | |
'user_id' => $userKey, | |
'legacy_id' => $legacyUser->id, | |
] | |
} | |
$userKey++; | |
$orderKey++; | |
} | |
\DB::table('users')->insert($this->users); | |
\DB::table('orders')->insert($this->orders); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment