public function indexCoupons($id)
 {
     $coupons = Coupon::where('garage_id', $id)->get();
     return view('back.garage.check.index_coupon', compact('coupons'));
 }
 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());
     }
 }
 /**
  * Display the specified resource.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function show($id)
 {
     $coupon = Coupon::findorFail($id);
     $brands = BrandLimit::lists('brand_name', 'brand_id');
     return view('back.coupon.check.show', compact('coupon', 'brands'));
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return \Illuminate\Http\Response
  */
 public function destroy($id)
 {
     Coupon::destroy($id);
     return redirect()->route('admin.coupon.manage.index')->with('message', '删除优惠成功!');
 }