ProtocolController.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Http\Controllers\Admins;
  3. use App\Modes\Protocol;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. class ProtocolController extends Controller
  7. {
  8. public function index(Request $request)
  9. {
  10. /*$validator = \Validator::make($param = $request->all(), [
  11. ]);
  12. if ($validator->fails()) {
  13. return showJsonErr($validator->errors()->first());
  14. }*/
  15. $res = Protocol::orderByDesc('created_at')
  16. ->with(['User' => function ($query) {
  17. $query->select(['nick_name', 'id']);
  18. }])
  19. ->limit(1)->get();
  20. return showJsonSucc(1001, $res);
  21. }
  22. public function add(Request $request)
  23. {
  24. $validator = \Validator::make($param = $request->all(), [
  25. 'title' => 'string|max:255',
  26. 'content' => 'required',
  27. ]);
  28. if ($validator->fails()) {
  29. return showJsonErr($validator->errors()->first());
  30. }
  31. $param['created_uid'] = \Auth::id();
  32. $param['status'] = 1;
  33. $protocol = Protocol::first();
  34. if (empty($protocol)) {
  35. return \DB::transaction(function () use ($param) {
  36. // Protocol::whereStatus(1)->update(['status' => 2]);
  37. $result = Protocol::insert($param);
  38. if (!$result) {
  39. return showJsonErr('添加协议失败');
  40. }
  41. return showJsonSucc('添加协议成功');
  42. });
  43. } else {
  44. $result = Protocol::whereId($protocol->id)->update($param);
  45. if (!$result) {
  46. return showJsonErr('修改协议失败');
  47. }
  48. return showJsonSucc('修改协议成功');
  49. }
  50. }
  51. }