Combining Models

Anyone know if my columns are the same, can I map from two connections into the same resource?
3 Replies
Abmael Souza
Abmael Souza2mo ago
i believe you're tring to do something like this
public static function query()
{
// Fetch data from the first connection
$connection1 = 'mysql';
$data1 = YourModel::on($connection1)->get();

// Fetch data from the second connection
$connection2 = 'mysql2';
$data2 = YourModel::on($connection2)->get();

// You can merge the results or return them separately
$mergedData = $data1->merge($data2); // Merging both collections

return $mergedData;
}
public static function query()
{
// Fetch data from the first connection
$connection1 = 'mysql';
$data1 = YourModel::on($connection1)->get();

// Fetch data from the second connection
$connection2 = 'mysql2';
$data2 = YourModel::on($connection2)->get();

// You can merge the results or return them separately
$mergedData = $data1->merge($data2); // Merging both collections

return $mergedData;
}
this will go on your resource and probably is what you want
Unsociable
UnsociableOP2mo ago
Thanks, will try it tomorrow!
Abmael Souza
Abmael Souza2mo ago
also, this is not native, id and stuff might bug out, so you better treat the data on the collections before passing to the return, so you can edit them, based on which db are they coming from. or add a new column to both tables on their db, for you to identify from which table they came from

Did you find this page helpful?