初始化
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Artisan;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\View\View;
|
||||
|
||||
/**
|
||||
* 后台数据库管理:查看迁移状态(已执行 / 待执行),并可一键执行迁移升级。
|
||||
* 满足「数据库可以随时在后台升级」——代码上线后无需 SSH,点按钮即可 migrate。
|
||||
*/
|
||||
class DatabaseController extends Controller
|
||||
{
|
||||
public function index(): View
|
||||
{
|
||||
$connection = config('database.default');
|
||||
$dbName = config("database.connections.{$connection}.database");
|
||||
|
||||
$ran = [];
|
||||
if (Schema::hasTable('migrations')) {
|
||||
$ran = DB::table('migrations')->pluck('migration')->toArray();
|
||||
}
|
||||
|
||||
$files = glob(database_path('migrations/*.php'));
|
||||
$all = array_map(fn ($f) => basename($f, '.php'), $files);
|
||||
sort($all);
|
||||
|
||||
$done = array_values(array_intersect($all, $ran));
|
||||
$pending = array_values(array_diff($all, $ran));
|
||||
|
||||
return view('admin.database', compact('connection', 'dbName', 'pending', 'done'));
|
||||
}
|
||||
|
||||
public function upgrade(Request $request): RedirectResponse
|
||||
{
|
||||
try {
|
||||
Artisan::call('migrate', ['--force' => true]);
|
||||
$output = Artisan::output();
|
||||
|
||||
return redirect()->route('admin.database')
|
||||
->with('success', '数据库升级已执行完成。')
|
||||
->with('migrate_output', $output);
|
||||
} catch (\Throwable $e) {
|
||||
return redirect()->route('admin.database')
|
||||
->with('error', '升级失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user