CouponController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Http\Controllers\Seller;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests\CouponRequest;
  5. use Illuminate\Http\Request;
  6. use App\Models\Coupon;
  7. use Auth;
  8. class CouponController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index()
  16. {
  17. $coupons = Coupon::where('user_id', Auth::user()->id)->orderBy('id','desc')->get();
  18. return view('seller.coupons.index', compact('coupons'));
  19. }
  20. /**
  21. * Show the form for creating a new resource.
  22. *
  23. * @return \Illuminate\Http\Response
  24. */
  25. public function create()
  26. {
  27. return view('seller.coupons.create');
  28. }
  29. /**
  30. * Store a newly created resource in storage.
  31. *
  32. * @param \Illuminate\Http\Request $request
  33. * @return \Illuminate\Http\Response
  34. */
  35. public function store(CouponRequest $request)
  36. {
  37. $user_id = Auth::user()->id;
  38. Coupon::create($request->validated() + [
  39. 'user_id' => $user_id,
  40. ]);
  41. flash(translate('Coupon has been saved successfully.'))->success();
  42. return redirect()->route('seller.coupon.index');
  43. }
  44. /**
  45. * Display the specified resource.
  46. *
  47. * @param int $id
  48. * @return \Illuminate\Http\Response
  49. */
  50. public function show($id)
  51. {
  52. //
  53. }
  54. /**
  55. * Show the form for editing the specified resource.
  56. *
  57. * @param int $id
  58. * @return \Illuminate\Http\Response
  59. */
  60. public function edit($id)
  61. {
  62. $coupon = Coupon::findOrFail(decrypt($id));
  63. return view('seller.coupons.edit', compact('coupon'));
  64. }
  65. /**
  66. * Update the specified resource in storage.
  67. *
  68. * @param \Illuminate\Http\Request $request
  69. * @param int $id
  70. * @return \Illuminate\Http\Response
  71. */
  72. public function update(CouponRequest $request, Coupon $coupon)
  73. {
  74. $coupon->update($request->validated());
  75. flash(translate('Coupon has been updated successfully'))->success();
  76. return redirect()->route('seller.coupon.index');
  77. }
  78. /**
  79. * Remove the specified resource from storage.
  80. *
  81. * @param int $id
  82. * @return \Illuminate\Http\Response
  83. */
  84. public function destroy($id)
  85. {
  86. Coupon::destroy($id);
  87. flash(translate('Coupon has been deleted successfully'))->success();
  88. return redirect()->route('seller.coupon.index');
  89. }
  90. public function get_coupon_form(Request $request)
  91. {
  92. if($request->coupon_type == "product_base") {
  93. $products = filter_products(\App\Models\Product::query()->from("products as p")->where('user_id', Auth::user()->id))->get();
  94. return view('partials.coupons.product_base_coupon', compact('products'));
  95. }
  96. elseif($request->coupon_type == "cart_base"){
  97. return view('partials.coupons.cart_base_coupon');
  98. }
  99. }
  100. public function get_coupon_form_edit(Request $request)
  101. {
  102. if($request->coupon_type == "product_base") {
  103. $coupon = Coupon::findOrFail($request->id);
  104. $products = filter_products(\App\Models\Product::query()->from("products as p")->where('user_id', Auth::user()->id))->get();
  105. return view('partials.coupons.product_base_coupon_edit',compact('coupon', 'products'));
  106. }
  107. elseif($request->coupon_type == "cart_base"){
  108. $coupon = Coupon::findOrFail($request->id);
  109. return view('partials.coupons.cart_base_coupon_edit',compact('coupon'));
  110. }
  111. }
  112. }