user()->id; $count = Promocode::where('used_count', $user_id)->count(); if (!$count) { $row = Promocode::where('used_count', 0)->first(); $row->used_count = $user_id; $row->save(); return response()->json(['status' => 'success', 'msg' => '已成功取得', 'promocode' => $row->code]); } else { $code = Promocode::where('used_count', $user_id)->latest() ->first(); ; return response()->json(['status' => 'success', 'msg' => '你已取得過優惠碼', 'promocode' => $code->code]); } } /** * Summary of morePromocode * @throws \Exception * @return mixed|\Illuminate\Http\JsonResponse */ public function morePromocode() { $user = Auth::guard('member')->user(); try { if ($user->level != 9) { throw new Exception("Error Processing Request", 401); } else { $row = Promocode::where('used_count', 0)->first(); $row->used_count = $user->id; $row->save(); return response()->json(['status' => 'success', 'msg' => '已成功取得', 'promocode' => $row->code]); } } catch (Exception $th) { //throw $th; return response()->json(['status' => 'error', 'msg' => $th->getMessage(), 'code' => $th->getCode()]); } } /** * Store a newly created resource in storage. */ public function store(Request $request) { // } /** * Display the specified resource. */ public function show(Promocode $promocode) { // } /** * Show the form for editing the specified resource. */ public function edit(Promocode $promocode) { // } /** * Update the specified resource in storage. */ public function update(Request $request, Promocode $promocode) { // } /** * Remove the specified resource from storage. */ public function destroy(Promocode $promocode) { // } }