public function useCoupon(Request $request) { $data = $request->all(); $coupon_code = $data['coupon_code']; try { // 根据输入的11位优惠码查找用户优惠券 $user_coupon = UserCoupon::where('coupon_code', $coupon_code)->first(); if (!$user_coupon) { throw new Exceptin('找不到优惠码对应的用户优惠券'); } // 优惠券是否可用(是否过期) if ($user_coupon->coupon_status != COUPON_STATUS_ENABLED) { dd($user_coupon->coupon_status); throw new Exception('优惠券不可用'); } if ($user_coupon->end_time > time()) { throw new Exception('优惠券已过期'); } $coupon = Coupon::find($user_coupon->coupon_id); if (!$coupon) { throw new Exception('找不到对应的优惠券'); } // 根据用户优惠券的coupon_id检验是否该车厂的优惠券 $garage = Garage::find($coupon->garage_id); if (!$garage) { throw new Exception('找不到对应的车厂'); } // 检查车厂的管理员id与当前登录的管理员id是否一致 // 不能用$coupon->admin_id与Auth::user()->id验证,因为$coupon->admin_id有可能是老的管理员 // 必须用$garage->admin_id与Auth::user()->id验证 if (Auth::user()->id != $garage->admin_id && !Entrust::hasRole('Admin')) { throw new Exception('你登陆的管理员账号与车厂管理员账号不匹配'); } // 设置用户优惠券状态为已使用 $user_coupon->update(['coupon_status' => COUPON_STATUS_USED]); return redirect()->route('admin.testgarage.index')->with('message', '成功使用模拟优惠券!'); } catch (Exception $e) { return redirect()->back()->withInput($request->input())->with('fail', '数据库操作返回异常!' . $e->getMessage()); } }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(CouponRequest $request, $id) { //根据id查询到需要更新的coupon $coupon = Coupon::find($id); //使用Eloquent的update()方法来更新,s //request的except()是排除某个提交过来的数据,我们这里排除id $coupon->update($request->except('id')); return redirect()->route('admin.managecoupon.index')->with('message', '修改优惠成功!'); }