I want to copy an eloquent model from one connection to another.
The way I have done this so far is as so:
$users = User::on('connection1')->where('tenant', 'foo')->get();
User::on('connection2')->insert($users->toArray());
This works most of the time. But there are cases where this does not work. For example:
$hidden
attributetoArray
method for a model is overridedWhat is a reliable way to simply copy over some rows to another connection?
i think i have found a solution for $hidden attribute & overwriting toArray method
$users = User::on('connection1')->where('tenant', 'foo')->get();
foreach ($users as $user) {
$usersArray[] = $user->getAttributes();
}
User::on('connection2')->insert($usersArray);
if you are using mySql i recommend using bulk insert:
https://www.geeksengine.com/database/data-manipulation/bulk-insert.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With