ProductController.php 2.7KB

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