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

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)

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝