$unit->id, 'system_key' => $templateRole['system_key'], 'scope_key' => "unit:{$unit->id}", 'slug' => \Illuminate\Support\Str::slug($templateRole['label'] . '-' . $unit->id), 'label' => $templateRole['label'], 'is_system' => false, 'access_scope' => 'unit', ]); foreach ($templateRole['permissions'] as $p) { \App\Models\UserTypePermission::create([ 'user_type' => $newRole->slug, 'user_type_id' => $newRole->id, 'permission_id' => $p['permission_id'], 'bits' => $p['bits'], ]); } } } REPLACE; // The original content to replace starts from "// Clona os perfis customizados" up to the end of the if statement $startString = "// Clona os perfis customizados (Neurotrainer, Financeiro, Gestor) baseados na unidade da Andreia"; $endString = " }\n\n \$franchisee = Franchisee::create(\$franchiseeData);"; $startPos = strpos($content, $startString); $endPos = strpos($content, $endString) + strlen(" }"); $contentToReplace = substr($content, $startPos, $endPos - $startPos); $content = str_replace($contentToReplace, $replacement, $content); file_put_contents('app/Services/UnitService.php', $content);