getOrDefault($setting); } } if (! function_exists('hasPermission')) { function hasPermission(string $permission): string { return app(PermissionsService::class)->getOrDefault($permission); } } if (! function_exists('hasHousekeepingPermission')) { function hasHousekeepingPermission(string $permission): string { return app(HousekeepingPermissionsService::class)->getOrDefault($permission); } } if (! function_exists('strLimit')) { function strLimit(string $string, int $limit, string $replacement = '...'): string { return Str::limit($string, $limit, $replacement); } } if (! function_exists('findMigration')) { function findMigration(string $tableName): string { static $cache = []; if (isset($cache[$tableName])) { return $cache[$tableName]; } $migrationsPath = database_path('migrations'); if (! is_dir($migrationsPath)) { return ''; } try { $files = new \DirectoryIterator($migrationsPath); foreach ($files as $file) { if ($file->isFile() && $file->getExtension() === 'php') { $content = @file_get_contents($file->getPathname()); if ($content !== false && str_contains($content, "Schema::create('$tableName'")) { $cache[$tableName] = $file->getFilename(); return $file->getFilename(); } } } } catch (\Exception $e) { \Illuminate\Support\Facades\Log::error("Error finding migration for table {$tableName}: {$e->getMessage()}"); } $cache[$tableName] = ''; return ''; } } if (! function_exists('columnExists')) { function columnExists(string $table, string $column): bool { return Schema::hasColumn($table, $column); } } if (! function_exists('dropForeignKeyIfExists')) { function dropForeignKeyIfExists(string $table, string $column): void { $connection = Schema::getConnection(); $tableWithPrefix = $connection->getTablePrefix() . $table; $foreignKeys = collect($connection->select('SELECT CONSTRAINT_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ?', [$tableWithPrefix, $column])) ->pluck('CONSTRAINT_NAME'); foreach ($foreignKeys as $foreignKey) { if (! empty($foreignKey)) { $connection->statement("ALTER TABLE {$table} DROP FOREIGN KEY {$foreignKey}"); } } } }