Updates trait, migration option added
This commit is contained in:
		| @@ -2,20 +2,27 @@ | ||||
|  | ||||
| namespace App\Traits; | ||||
|  | ||||
| use Illuminate\Support\Facades\Artisan; | ||||
| use Illuminate\Support\Facades\File; | ||||
|  | ||||
| trait Updates | ||||
| { | ||||
|     /** | ||||
|      * get the alias. | ||||
|      */ | ||||
|     public function getAlias(): string | ||||
|     { | ||||
|         $const = static::class . '::ALIAS'; | ||||
|  | ||||
|         return defined($const) ? constant($const) : 'core'; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * get the path by alias. | ||||
|      */ | ||||
|     public function getPathByAlias(string $alias = ''): string | ||||
|     { | ||||
|         if (empty($alias)) { | ||||
|             $const = static::class . '::ALIAS'; | ||||
|  | ||||
|             $alias = defined($const) ? constant($const) : 'core'; | ||||
|         } | ||||
|         $alias = empty($alias) ? $this->getAlias() : $alias; | ||||
|  | ||||
|         return $alias === 'core' ? base_path() : module_path($alias); | ||||
|     } | ||||
| @@ -43,4 +50,28 @@ trait Updates | ||||
|             File::deleteDirectory($path . '/' . $folder); | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     public function runMigration(string $file_name, $alias = ''): void | ||||
|     { | ||||
|         $path = $this->getPathByAlias($alias); | ||||
|  | ||||
|         $semi_path = str($path)->contains('modules') ? '/Database/Migrations/' : '/database/migrations/'; | ||||
|  | ||||
|         $migration_file = $path . $semi_path . $file_name . '.php'; | ||||
|  | ||||
|         if (File::missing($migration_file)) { | ||||
|             return; | ||||
|         } | ||||
|  | ||||
|         $migration_file = str($migration_file)->replace(base_path(), ''); | ||||
|  | ||||
|         Artisan::call('migrate', ['--force' => true, '--path' => $migration_file]); | ||||
|     } | ||||
|  | ||||
|     public function runMigrations(array $file_names, $alias = ''): void | ||||
|     { | ||||
|         foreach ($file_names as $file_name) { | ||||
|             $this->runMigration($file_name, $alias); | ||||
|         } | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user