getOrDefault($key, $default); } } if (! function_exists('hasPermission')) { function hasPermission(string $permission): bool { return (bool) app(PermissionsService::class)->getOrDefault($permission); } } if (! function_exists('hasHousekeepingPermission')) { function hasHousekeepingPermission(string $permission): bool { return (bool) 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 { // Iterate through all migration files in the migrations directory foreach (glob(database_path('migrations/*.php')) as $filename) { // Check if the migration file has the Schema::create() line with the given table name $content = file_get_contents($filename); if (preg_match("/Schema::create\\(['\"]".preg_quote($tableName, '/')."['\"]/", $content)) { return basename($filename, '.php'); } } throw new RuntimeException("Migration for table '{$tableName}' not found."); } } if (! function_exists('getPredominantImageColor')) { function getPredominantImageColor(string $imagePath): string { // Stub implementation - returns a default color // In production, this would extract the predominant color from the image return '#000000'; } } if (! function_exists('columnExists')) { function columnExists(string $table, string $column): bool { try { return Schema::hasColumn($table, $column); } catch (Throwable) { return false; } } } 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}"); } } } }