CustomerController.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\Customer\StoreCustomerRequest;
  5. use App\Http\Requests\Customer\UpdateCustomerRequest;
  6. use App\Models\Customer;
  7. class CustomerController 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('customer/Index', [
  17. 'customers' => Customer::latest()
  18. ->filter(request()->search)
  19. ->paginate(10)
  20. ->withQueryString()
  21. ->through(fn($customer) => [
  22. 'id' => $customer->id,
  23. 'customer_number' => $customer->customer_number,
  24. 'name' => $customer->name,
  25. 'phone' => $customer->phone,
  26. 'address' => $customer->address,
  27. 'gender' => $customer->gender_id,
  28. ]),
  29. ]);
  30. }
  31. /**
  32. * Show the form for creating a new resource.
  33. *
  34. * @return \Inertia\Response
  35. */
  36. public function create()
  37. {
  38. return inertia('customer/Create', [
  39. 'customer_number' => 'CS' . now()->format('YmdHis'),
  40. 'genders' => [
  41. ['label' => 'Perempuan', 'value' => 1],
  42. ['label' => 'Laki-laki', 'value' => 2],
  43. ],
  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(StoreCustomerRequest $request)
  53. {
  54. Customer::create($request->validated());
  55. return to_route('customers.index')->with('success', __('messages.success.store.customer'));
  56. }
  57. /**
  58. * Display the specified resource.
  59. *
  60. * @param int $id
  61. * @return \Inertia\Response
  62. */
  63. public function show($id)
  64. {
  65. //
  66. }
  67. /**
  68. * Show the form for editing the specified resource.
  69. *
  70. * @param Customer $customer
  71. * @return \Inertia\Response
  72. */
  73. public function edit(Customer $customer)
  74. {
  75. return inertia('customer/Edit', [
  76. 'customer' => [
  77. 'id' => $customer->id,
  78. 'customer_number' => $customer->customer_number,
  79. 'name' => $customer->name,
  80. 'phone' => $customer->phone,
  81. 'address' => $customer->address,
  82. 'gender_id' => $customer->getRawOriginal('gender_id'),
  83. ],
  84. 'genders' => [
  85. ['label' => 'Perempuan', 'value' => 1],
  86. ['label' => 'Laki-laki', 'value' => 2],
  87. ],
  88. ]);
  89. }
  90. /**
  91. * Update the specified resource in storage.
  92. *
  93. * @param \Illuminate\Http\Request $request
  94. * @param Customer $customer
  95. * @return \Illuminate\Http\Response
  96. */
  97. public function update(UpdateCustomerRequest $request, Customer $customer)
  98. {
  99. $customer->update($request->validated());
  100. return back()->with('success', __('messages.success.update.customer'));
  101. }
  102. /**
  103. * Remove the specified resource from storage.
  104. *
  105. * @param Customer $customer
  106. * @return \Illuminate\Http\Response
  107. */
  108. public function destroy(Customer $customer)
  109. {
  110. $customer->delete();
  111. return to_route('customers.index')->with('success', __('messages.success.destroy.customer'));
  112. }
  113. }