du5114
2016-05-31 09:27
浏览 354
已采纳

Laravel 5.1 - 检查日期是否过期

i'm doing an ecommerce, i created:

-Session "cart" with all products attributes ( price, id, quantity, category etc)

-CouponController.php

namespace dixard\Http\Controllers;

use Illuminate\Http\Request;

use dixard\Http\Requests;
use dixard\Http\Controllers\Controller;

use dixard\Coupon;
use dixard\Http\Controllers\Carbon\Carbon;

class CouponController extends Controller

public function postCoupon(Request $request)
        {

            $cart = \Session::get('cart');

            $mytime = Carbon\Carbon::now(); // today

            // i check if code coupon exist into my DB

            $coupon = Coupon::where('code', $request->get('coupon'))->first();

            if (!empty($coupon) && $coupon->expire_date ) {


                // i need check IF coupon exist AND date not expired --> i will put a new price into my session cart products.  

            }

        }

Model Coupon.php

protected $table = 'coupons';

    protected $fillable = [

    'code', // code of coupon
    'price', // price discount
    'expire_date', // expire date of coupon

    ];

MY QUESTION

I would like:

  1. Check with my CouponController if the code coupon is expired or not
  2. If expired return a simply message "Coupon invalid"
  3. If Coupon code exist into my DB and NOT expired, i need create a new variable with price value of coupon , for example "$discount = Coupon->price", so i can pass my discount price to my checkout view.

How can i do this ?

Thank you for yout help! ( i read something about carbon, but i dont undestand fine)

图片转代码服务由CSDN问答提供 功能建议

我正在做电子商务,我创建了:

< p> -Session“cart”包含所有产品属性(价格,内容,数量,类别等)

-CouponController.php

  namespace dixard \ Http \ Controllers; 
 
use Illuminate \ Http \ Request; 
 
use \ tdixard \ Http \ Requests; 
use \ tixard \ Http \ Controllers \ Controller; 
 
use \ tdixard \ Coupon  ; 
use dixard \ Http \ Controllers \ Carbon \ Carbon; 
 
class CouponController扩展Controller 
 
公共函数postCoupon(Request $ request)
 {
 
 $ cart = \ Session :: get('cart'  ); 
 
 $ mytime = Carbon \ Carbon :: now();  //今天
 
 //我检查我的数据库中是否存在代码优惠券
 
 $ coupon = Coupon :: where('code',$ request-&gt; get('coupon')) - &gt; first  (); 
 
 if if(!empty($ coupon)&amp;&amp; $ coupon-&gt; expire_date){
 
 
 //我需要检查IF优惠券是否存在且日期未过期 - &gt; 我会在我的会话购物车产品中加入新价格。  
 
} 
 
} 
   
 
 

Model Coupon.php

  protected $ table ='coupons'; 
 
 protected $ fillable = [
 
'code',//优惠券代码
'价格',//价格折扣
'expire_date  ',//过期优惠券日期
 
]; 
   
 
 

我的问题

我想:

  1. 使用我的CouponController检查代码优惠券是否已过期
  2. 如果已过期,则返回简单消息“优惠券无效” \ n
  3. 如果优惠券代码存在于我的数据库中且未过期,我需要创建一个价值为优惠券的新变量,例如“$ discount = Coupon-&gt; price”,这样我就可以将折扣价格传递给我的结账 查看。

    我该怎么做?

    谢谢你的帮助! (我读了一些关于碳的东西,但我并不准备好)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dsfdf854456 2016-05-31 09:41
    已采纳

    If $coupon->expire_date is not instance of Carbon already, make it (new Carbon($coupon->expire_date), then simply compare those two objects as if they were numbers.

    For example (assuming that $coupon->expire_date is instance of Carbon:

    if ($coupon->expire_date >= $my_time) {
      // ok
    } else {
      // error, coupon expired
    }
    

    Carbon is very handy for all sorts of comparisons, calculating differences etc. Here you can find loads of examples.

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douwu8060 2016-05-31 09:42

    Use date mutators in order to make expire_date field a Carbon instance automatically.

    class Coupon extends Model
    {
        /**
         * The attributes that should be mutated to dates.
         *
         * @var array
         */
        protected $dates = ['expire_date'];
    }
    

    So after that I think you only need to check if the expiring date of coupon is a future date.

    if (!empty($coupon) && $coupon->expire_date->isFuture() ) {
        // valid coupon
    }
    else{
        return redirect()->back()->withErrors(['coupon' => 'Coupon is not valid']);
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题