I have this command, which with an array of code => name, 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();
}
});
The questions is that is other more efficent way to do this rather than doing two loops and update query in every record
Thanks!!