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条)

报告相同问题?

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)