I want to increase speed and reduce queries in this command, i try to do two loops and update query in every record, is other more efficient way to do this?
The command have an array of code => name, that looks for the similar or best matching name with another name filed in another model, then updates the other model
$similarityThreshold = 70;
$tolls->each(function($toll) use ($partnerCodes, $similarityThreshold) {
$nameField = $toll->vat_service_route_name;
$bestMatch = null;
$bestSimilarity = 0;
// Search best match
foreach ($partnerCodes as $name => $partnerCode) {
similar_text($nameField, $name, $similarity);
if ($similarity > $bestSimilarity) {
$bestSimilarity = $similarity;
$bestMatch = $partnerCode;
}
}
// If find best match, update partner_code.
if ($bestSimilarity > $similarityThreshold) {
$toll->partner_code = $bestMatch;
$toll->save();
}
});
Its possible to update all models in one query? rather than doing multiple queries?
Thanks!!
4