TopUpController.php 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\TopUp\StoreTopUpRequest;
  4. use App\Http\Requests\TopUp\UpdateTopUpRequest;
  5. use App\Models\Member;
  6. use App\Models\TopUp;
  7. use Inertia\Inertia;
  8. class TopUpController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Inertia\Response
  14. */
  15. public function index()
  16. {
  17. return inertia('topup/Index', [
  18. 'filters' => request()->all('startDate', 'endDate', 'search'),
  19. 'topUp' => TopUp::filter(request()->only('startDate', 'endDate', 'search'))
  20. ->latest()
  21. ->paginate(10)
  22. ->withQueryString()
  23. ->through(fn($topUp) => [
  24. 'id' => $topUp->id,
  25. 'updatedAt' => $topUp->updated_at,
  26. 'name' => $topUp->member->name,
  27. 'phone' => $topUp->member->phone,
  28. 'platNumber' => $topUp->member->vehicleDetail(),
  29. 'amount' => $topUp->amount,
  30. 'type' => $topUp->member->typeMember->type,
  31. 'expDate' => $topUp->exp_date,
  32. ]),
  33. ]);
  34. }
  35. /**
  36. * Show the form for creating a new resource.
  37. *
  38. * @return \Inertia\Response
  39. */
  40. public function create()
  41. {
  42. return inertia('topup/Create', [
  43. 'members' => Inertia::lazy(
  44. fn() => Member::filter(request()->only('search'))->latest()->get()->transform(fn($member) => [
  45. 'id' => $member->id,
  46. 'name' => $member->name,
  47. 'phone' => $member->phone,
  48. 'platNumber' => $member->vehicleDetail(),
  49. 'type' => $member->typeMember->type,
  50. 'expDate' => $member->exp_date,
  51. ])
  52. ),
  53. ]);
  54. }
  55. /**
  56. * Store a newly created resource in storage.
  57. *
  58. * @param \Illuminate\Http\Request $request
  59. * @return \Illuminate\Http\Response
  60. */
  61. public function store(StoreTopUpRequest $request)
  62. {
  63. TopUp::create([
  64. 'balance' => $request->balance,
  65. 'exp_date' => $request->exp_date,
  66. 'member_id' => $request->member_id,
  67. 'user_id' => auth()->user()->id,
  68. ]);
  69. return back()->with('success', __('messages.success.store.top_up'));
  70. }
  71. /**
  72. * Display the specified resource.
  73. *
  74. * @param TopUp $topUp
  75. * @return \Inertia\Response
  76. */
  77. public function show(TopUp $topUp)
  78. {
  79. return inertia('topup/Show', [
  80. 'topUp' => [
  81. 'updatedAt' => $topUp->updated_at,
  82. 'name' => $topUp->member->name,
  83. 'phone' => $topUp->member->phone,
  84. 'platNumber' => $topUp->member->plat_number,
  85. 'balance' => $topUp->balance,
  86. 'expDate' => $topUp->exp_date,
  87. 'user' => [
  88. 'name' => $topUp->user->name,
  89. 'phone' => $topUp->user->phone,
  90. 'email' => $topUp->user->email,
  91. ],
  92. ],
  93. ]);
  94. }
  95. /**
  96. * Show the form for editing the specified resource.
  97. *
  98. * @param TopUp $topUp
  99. * @return \Inertia\Response
  100. */
  101. public function edit(TopUp $topUp)
  102. {
  103. return inertia('topup/Index', [
  104. 'topUp' => [
  105. 'balance' => $topUp->balance,
  106. 'exp_date' => $topUp->balance,
  107. ],
  108. ]);
  109. }
  110. /**
  111. * Update the specified resource in storage.
  112. *
  113. * @param \Illuminate\Http\Request $request
  114. * @param TopUp $topUp
  115. * @return \Illuminate\Http\Response
  116. */
  117. public function update(UpdateTopUpRequest $request, TopUp $topUp)
  118. {
  119. $topUp->update($request->validated());
  120. return back()->with('success', __('messages.success.update.top_up'));
  121. }
  122. /**
  123. * Remove the specified resource from storage.
  124. *
  125. * @param TopUp $topUp
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function destroy(TopUp $topUp)
  129. {
  130. $topUp->delete();
  131. return to_route('users.index')->with('success', __('messages.success.destroy.top_up'));
  132. }
  133. }