简体   繁体   English

Laravel 5.1 - 检查日期是否过期

[英]Laravel 5.1 - Checking if the date expired

i'm doing an ecommerce, i created:我正在做电子商务,我创建了:

-Session "cart" with all products attributes ( price, id, quantity, category etc) -具有所有产品属性(价格、ID、数量、类别等)的会话“购物车”

-CouponController.php -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模特优惠券.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与我的 CouponController 检查代码优惠券是否已过期
  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.如果优惠券代码存在于我的数据库中并且没有过期,我需要创建一个带有优惠券价格值的新变量,例如“$discount = Coupon->price”,这样我就可以将我的折扣价格传递给我的结帐视图。

How can i do this ?我怎样才能做到这一点 ?

Thank you for yout help!谢谢你的帮助! ( i read something about carbon, but i dont undestand fine) (我读了一些关于碳的东西,但我不明白)

Use date mutators in order to make expire_date field a Carbon instance automatically. 使用日期修改器 ,以使expire_date字段自动成为Carbon实例。

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']);
}

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. 如果$coupon->expire_date还不是Carbon实例,请将其new Carbon($coupon->expire_date)new Carbon($coupon->expire_date) ,然后简单地比较这两个对象,就好像它们是数字一样。

For example (assuming that $coupon->expire_date is instance of Carbon : 例如(假设$coupon->expire_dateCarbon实例:

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. Carbon非常适合进行各种比较,计算差异等。 在这里您可以找到大量示例。

    $now = Carbon::now();
    $startDate = Carbon::parse($yourModelObject['created_at'])->format('d.m.Y h:m:sa');
    $endDate = Carbon::parse($yourModelObject['created_at'])->addMinutes(720)->format('d.m.Y h:m:sa');

    if ($now->between($startDate, $endDate)) {
        return 'Date is Active';
    } else {
        return 'Date is Expired';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM