mardi 31 mai 2016

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)

-CouponController.php

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)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire