ProductController.php 2.8KB

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