NestsController.php 3.6 KB

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