/** * @param string $code * @return Coupon * @throws RestException */ private function getCoupon($code) { try { return Coupon::where('code', 'ilike', $code)->firstOrFail(); } catch (ModelNotFoundException $e) { throw new RestException(HTTP_NOT_FOUND, 'Coupon not found', ['coupon' => $code]); } }
public function setCouponIdAttribute($id) { if ($id == $this->coupon_id) { //nothing to do here, let's skip all the queries! return; } $discount = $id ? Coupon::findOrFail($id)->discount : 0; foreach ($this->items as $item) { $item->setDiscount($discount); $item->save(); } $this->attributes['coupon_id'] = $id; }