123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Laundry\StoreLaundryRequest;
  5. use App\Http\Requests\Laundry\UpdateLaundryRequest;
  6. use App\Models\Laundry;
  7. class LaundryController extends Controller
  8. {
  9. /**
  10. * Create the controller instance.
  11. *
  12. * @return void
  13. */
  14. public function __construct()
  15. {
  16. $this->authorizeResource(Laundry::class);
  17. }
  18. /**
  19. * Display a listing of the resource.
  20. *
  21. * @return \Inertia\Response
  22. */
  23. public function index()
  24. {
  25. return inertia('laundry/Index', [
  26. 'filters' => request()->all('search'),
  27. 'laundries' => Laundry::filter(request()->only('search'))
  28. ->latest()
  29. ->paginate(10)
  30. ->withQueryString()
  31. ->through(fn($laundry) => [
  32. 'id' => $laundry->id,
  33. 'name' => $laundry->name,
  34. 'price' => $laundry->price,
  35. 'unit' => $laundry->unit,
  36. ]),
  37. ]);
  38. }
  39. /**
  40. * Show the form for creating a new resource.
  41. *
  42. * @return \Inertia\Response
  43. */
  44. public function create()
  45. {
  46. return inertia('laundry/Create');
  47. }
  48. /**
  49. * Store a newly created resource in storage.
  50. *
  51. * @param \Illuminate\Http\Request $request
  52. * @return \Illuminate\Http\Response
  53. */
  54. public function store(StoreLaundryRequest $request)
  55. {
  56. Laundry::create($request->validated());
  57. return back()->with('success', __('messages.success.store.laundry'));
  58. }
  59. /**
  60. * Display the specified resource.
  61. *
  62. * @param int $id
  63. * @return \Inertia\Response
  64. */
  65. public function show($id)
  66. {
  67. //
  68. }
  69. /**
  70. * Show the form for editing the specified resource.
  71. *
  72. * @param Laundry $laundry
  73. * @return \Inertia\Response
  74. */
  75. public function edit(Laundry $laundry)
  76. {
  77. return inertia('laundry/Edit', [
  78. 'laundry' => [
  79. 'id' => $laundry->id,
  80. 'name' => $laundry->name,
  81. 'price' => $laundry->getRawOriginal('price'),
  82. 'unit' => $laundry->unit,
  83. 'relation' => $laundry->transactionDetails()->exists(),
  84. ],
  85. ]);
  86. }
  87. /**
  88. * Update the specified resource in storage.
  89. *
  90. * @param \Illuminate\Http\Request $request
  91. * @param Laundry $laundry
  92. * @return \Illuminate\Http\Response
  93. */
  94. public function update(UpdateLaundryRequest $request, Laundry $laundry)
  95. {
  96. $laundry->update($request->validated());
  97. return back()->with('success', __('messages.success.update.laundry'));
  98. }
  99. /**
  100. * Remove the specified resource from storage.
  101. *
  102. * @param Laundry $laundry
  103. * @return \Illuminate\Http\Response
  104. */
  105. public function destroy(Laundry $laundry)
  106. {
  107. $laundry->delete();
  108. return to_route('laundries.index')->with('success', __('messages.success.destroy.laundry'));
  109. }
  110. }