ProductController.php 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. public function __construct()
  10. {
  11. $this->authorizeResource(Product::class);
  12. }
  13. /**
  14. * Display a listing of the resource.
  15. *
  16. * @return \Illuminate\Http\Response
  17. */
  18. public function index()
  19. {
  20. return inertia('Products/Index', [
  21. 'initialSearch' => request('search'),
  22. 'products' => Product::filter(request()->only('search'))
  23. ->latest()
  24. ->paginate(10)
  25. ->withQueryString()
  26. ->through(fn($product) => [
  27. 'id' => $product->id,
  28. 'number' => $product->number,
  29. 'name' => $product->name,
  30. 'unit' => $product->unit,
  31. 'isUsed' => ProductService::isUsed($product)
  32. ])
  33. ]);
  34. }
  35. /**
  36. * Show the form for creating a new resource.
  37. *
  38. * @return \Illuminate\Http\Response
  39. */
  40. public function create()
  41. {
  42. return inertia('Products/Create', [
  43. 'number' => 'PDK' . now()->format('YmdHis')
  44. ]);
  45. }
  46. /**
  47. * Store a newly created resource in storage.
  48. *
  49. * @param \Illuminate\Http\Request $request
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function store(StoreProductRequest $request)
  53. {
  54. Product::create($request->validated());
  55. return back()->with('success', __('messages.success.store.product'));
  56. }
  57. /**
  58. * Display the specified resource.
  59. *
  60. * @param Product $product
  61. * @return \Illuminate\Http\Response
  62. */
  63. public function show(Product $product)
  64. {
  65. //
  66. }
  67. /**
  68. * Show the form for editing the specified resource.
  69. *
  70. * @param Product $product
  71. * @return \Illuminate\Http\Response
  72. */
  73. public function edit(Product $product)
  74. {
  75. return inertia('Products/Edit', compact('product'));
  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 back()->with('success', __('messages.success.destroy.product'));
  99. }
  100. }