NestsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Egg;
  5. use App\Models\Nest;
  6. use Exception;
  7. use Illuminate\Contracts\Foundation\Application;
  8. use Illuminate\Contracts\View\Factory;
  9. use Illuminate\Contracts\View\View;
  10. use Illuminate\Http\JsonResponse;
  11. use Illuminate\Http\RedirectResponse;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Http\Response;
  14. /**
  15. * @deprecated
  16. * Class NestsController
  17. * @package App\Http\Controllers\Admin
  18. */
  19. class NestsController extends Controller
  20. {
  21. /**
  22. * Display a listing of the resource.
  23. *
  24. * @return Application|Factory|View
  25. */
  26. public function index()
  27. {
  28. return view('admin.nests.index');
  29. }
  30. /**
  31. * Show the form for creating a new resource.
  32. *
  33. * @return Response
  34. */
  35. public function create()
  36. {
  37. //
  38. }
  39. /**
  40. * Store a newly created resource in storage.
  41. *
  42. * @param Request $request
  43. * @return Response
  44. */
  45. public function store(Request $request)
  46. {
  47. //
  48. }
  49. /**
  50. * Display the specified resource.
  51. *
  52. * @param Nest $nest
  53. * @return Response
  54. */
  55. public function show(Nest $nest)
  56. {
  57. //
  58. }
  59. /**
  60. * Show the form for editing the specified resource.
  61. *
  62. * @param Nest $nest
  63. * @return Response
  64. */
  65. public function edit(Nest $nest)
  66. {
  67. //
  68. }
  69. /**
  70. * Update the specified resource in storage.
  71. *
  72. * @param Request $request
  73. * @param Nest $nest
  74. * @return RedirectResponse
  75. */
  76. public function update(Request $request, Nest $nest)
  77. {
  78. $disabled = !!is_null($request->input('disabled'));
  79. $nest->update(['disabled' => $disabled]);
  80. return redirect()->back()->with('success', 'Nest updated');
  81. }
  82. /**
  83. * Remove the specified resource from storage.
  84. *
  85. * @param Nest $nest
  86. * @return Response
  87. */
  88. public function destroy(Nest $nest)
  89. {
  90. //
  91. }
  92. /**
  93. *
  94. * @throws Exception
  95. */
  96. public function sync(){
  97. Egg::query()->delete();
  98. Nest::query()->delete();
  99. Nest::syncNests();
  100. Egg::syncEggs();
  101. return redirect()->back()->with('success', 'Nests and Eggs have been synced');
  102. }
  103. /**
  104. * @param Request $request
  105. * @return JsonResponse|mixed
  106. * @throws Exception
  107. */
  108. public function dataTable(Request $request)
  109. {
  110. $query = Nest::with(['eggs']);
  111. $query->select('nests.*');
  112. return datatables($query)
  113. ->addColumn('eggs', function (Nest $nest) {
  114. return $nest->eggs()->count();
  115. })
  116. ->addColumn('actions', function (Nest $nest) {
  117. $checked = $nest->disabled == false ? "checked" : "";
  118. return '
  119. <form class="d-inline" onsubmit="return submitResult();" method="post" action="' . route('admin.nests.update', $nest->id) . '">
  120. ' . csrf_field() . '
  121. ' . method_field("PATCH") . '
  122. <div class="custom-control custom-switch">
  123. <input '.$checked.' name="disabled" onchange="this.form.submit()" type="checkbox" class="custom-control-input" id="switch'.$nest->id.'">
  124. <label class="custom-control-label" for="switch'.$nest->id.'"></label>
  125. </div>
  126. </form>
  127. ';
  128. })
  129. ->editColumn('created_at' , function (Nest $nest) {
  130. return $nest->created_at ? $nest->created_at->diffForHumans() : '';
  131. })
  132. ->rawColumns(['actions'])
  133. ->make();
  134. }
  135. }