Lae/app/Http/Controllers/Admin/HostController.php

94 lines
2.3 KiB
PHP
Raw Normal View History

2022-11-18 09:16:30 +00:00
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
2022-11-18 10:50:28 +00:00
use App\Models\Host;
use Illuminate\Http\RedirectResponse;
2022-11-18 09:16:30 +00:00
use Illuminate\Http\Request;
2022-11-18 11:39:30 +00:00
use Illuminate\View\View;
2022-11-18 09:16:30 +00:00
class HostController extends Controller
{
/**
* Display a listing of the resource.
*
2022-11-19 06:04:42 +00:00
*
2023-02-07 09:03:47 +00:00
* @param Request $request
*
2022-11-19 06:04:42 +00:00
* @return View
2022-11-18 09:16:30 +00:00
*/
2022-12-18 10:08:46 +00:00
public function index(Request $request): View
2022-11-18 09:16:30 +00:00
{
2023-01-28 19:11:28 +00:00
$hosts = new Host;
2022-12-18 10:08:46 +00:00
// 遍历所有的搜索条件
2022-12-18 10:14:07 +00:00
foreach (['name', 'module_id', 'status', 'user_id', 'price', 'managed_price', 'created_at', 'updated_at'] as $field) {
2022-12-18 10:08:46 +00:00
if ($request->has($field)) {
2023-02-07 09:03:47 +00:00
$hosts = $hosts->where($field, 'like', '%' . $request->input($field) . '%');
2022-12-18 10:08:46 +00:00
}
}
2023-01-28 18:40:11 +00:00
$hosts = $hosts->with(['user', 'module']);
2022-12-27 16:25:22 +00:00
$hosts = $hosts->paginate(50)->withQueryString();
2022-11-18 09:16:30 +00:00
2022-11-18 10:50:28 +00:00
return view('admin.hosts.index', compact('hosts'));
2022-11-18 09:16:30 +00:00
}
/**
* Show the form for editing the specified resource.
*
2023-02-07 09:03:47 +00:00
* @param Host $host
*
2022-11-19 06:04:42 +00:00
* @return View
2022-11-18 09:16:30 +00:00
*/
2022-11-18 11:39:30 +00:00
public function edit(Host $host): View
2022-11-18 09:16:30 +00:00
{
2022-11-18 11:39:30 +00:00
return view('admin.hosts.edit', compact('host'));
2022-11-18 09:16:30 +00:00
}
/**
* Update the specified resource in storage.
*
2023-02-07 09:03:47 +00:00
* @param Request $request
* @param Host $host
*
* @return RedirectResponse
2022-11-18 09:16:30 +00:00
*/
public function update(Request $request, Host $host): RedirectResponse
2022-11-18 09:16:30 +00:00
{
2022-11-18 11:39:30 +00:00
$request->validate([
2023-01-13 10:18:49 +00:00
'name' => 'sometimes|string|max:255',
'status' => 'sometimes|in:running,stopped,suspended,pending,locked,unavailable',
2023-01-13 10:18:49 +00:00
'price' => 'sometimes|numeric',
2022-11-18 11:39:30 +00:00
'managed_price' => 'nullable|numeric',
]);
2023-01-13 10:18:15 +00:00
$host->update($request->all());
2022-11-18 11:39:30 +00:00
return back()->with('success', '此主机已更新。');
2022-11-18 09:16:30 +00:00
}
/**
* Remove the specified resource from storage.
*
2023-02-07 09:03:47 +00:00
* @param Host $host
*
* @return RedirectResponse
2022-11-18 09:16:30 +00:00
*/
public function destroy(Host $host): RedirectResponse
2022-11-18 09:16:30 +00:00
{
$host->safeDelete();
return redirect()->route('admin.hosts.index')->with('success', '正在排队删除此主机。');
2022-11-18 09:16:30 +00:00
}
2023-01-13 10:42:05 +00:00
public function updateOrDelete(Host $host): RedirectResponse
{
$host->updateOrDelete();
return back()->with('success', '正在排队刷新此主机的状态。');
}
2022-11-18 09:16:30 +00:00
}