ConnectionController.php•2.98 kB
<?php
namespace App\Http\Controllers;
use App\Models\SubjectConnection;
use Illuminate\Http\Request;
class ConnectionController extends Controller
{
public function index()
{
$connections = SubjectConnection::orderBy('connection_name')->get();
return view('connections.index', compact('connections'));
}
public function create()
{
return view('connections.create');
}
public function store(Request $request)
{
$validated = $request->validate([
'connection_name' => 'required|string|max:255|unique:subject_connections',
'driver' => 'required|in:mysql,pgsql,sqlite,sqlsrv',
'host' => 'nullable|string|max:255',
'port' => 'nullable|integer|min:1|max:65535',
'database' => 'required|string|max:255',
'username' => 'nullable|string|max:255',
'password' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean'
]);
SubjectConnection::create($validated);
return redirect()->route('connections.index')
->with('success', 'Connection created successfully.');
}
public function show(SubjectConnection $connection)
{
return view('connections.show', compact('connection'));
}
public function edit(SubjectConnection $connection)
{
return view('connections.edit', compact('connection'));
}
public function update(Request $request, SubjectConnection $connection)
{
$validated = $request->validate([
'connection_name' => 'required|string|max:255|unique:subject_connections,connection_name,' . $connection->id,
'driver' => 'required|in:mysql,pgsql,sqlite,sqlsrv',
'host' => 'nullable|string|max:255',
'port' => 'nullable|integer|min:1|max:65535',
'database' => 'required|string|max:255',
'username' => 'nullable|string|max:255',
'password' => 'nullable|string|max:255',
'description' => 'nullable|string',
'is_active' => 'boolean'
]);
$connection->update($validated);
return redirect()->route('connections.index')
->with('success', 'Connection updated successfully.');
}
public function destroy(SubjectConnection $connection)
{
$connection->delete();
return redirect()->route('connections.index')
->with('success', 'Connection deleted successfully.');
}
public function test(SubjectConnection $connection)
{
try {
$dbConnection = $connection->getDatabaseConnection();
$dbConnection->getPdo();
return back()->with('success', "Connection '{$connection->connection_name}' tested successfully.");
} catch (\Exception $e) {
return back()->with('error', "Connection test failed: " . $e->getMessage());
}
}
}