v2board/app/Http/Controllers/Admin/PlanController.php
Tokumeikoi 5ee6fc2996 opt
2020-02-23 02:37:52 +08:00

84 lines
2.1 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\Admin\PlanSave;
use App\Http\Requests\Admin\PlanUpdate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Plan;
use App\Models\Order;
use App\Models\User;
class PlanController extends Controller
{
public function fetch(Request $request)
{
return response([
'data' => Plan::get()
]);
}
public function save(PlanSave $request)
{
$params = $request->only(array_keys(PlanSave::RULES));
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
abort(500, '该订阅不存在');
}
if (!$plan->update($params)) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
if (!Plan::create($params)) {
abort(500, '创建失败');
}
return response([
'data' => true
]);
}
public function drop(Request $request)
{
if (Order::where('plan_id', $request->input('id'))->first()) {
abort(500, '该订阅下存在订单无法删除');
}
if (User::where('plan_id', $request->input('id'))->first()) {
abort(500, '该订阅下存在用户无法删除');
}
if ($request->input('id')) {
$plan = Plan::find($request->input('id'));
if (!$plan) {
abort(500, '该订阅ID不存在');
}
}
return response([
'data' => $plan->delete()
]);
}
public function update(PlanUpdate $request)
{
$updateData = $request->only([
'show',
'renew'
]);
$plan = Plan::find($request->input('id'));
if (!$plan) {
abort(500, '该订阅不存在');
}
if (!$plan->update($updateData)) {
abort(500, '保存失败');
}
return response([
'data' => true
]);
}
}