Laravel 5.1 - 检查日期是否过期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37541854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 - Checking if the date expired
提问by Diego Cespedes
i'm doing an ecommerce, i created:
我正在做电子商务,我创建了:
-Session "cart" with all products attributes ( price, id, quantity, category etc)
-CouponController.php
-具有所有产品属性(价格、ID、数量、类别等)的会话“购物车”
-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:
我想:
- Check with my CouponController if the code coupon is expired or not
- If expired return a simply message "Coupon invalid"
- 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.
- 请与我的 CouponController 检查代码优惠券是否已过期
- 如果过期返回一个简单的消息“优惠券无效”
- 如果优惠券代码存在于我的数据库中并且没有过期,我需要创建一个带有优惠券价格值的新变量,例如“$discount = Coupon->price”,这样我就可以将我的折扣价格传递给我的结帐视图。
How can i do this ?
我怎样才能做到这一点 ?
Thank you for yout help! ( i read something about carbon, but i dont undestand fine)
谢谢你的帮助!(我读了一些关于碳的东西,但我不明白)
采纳答案by Oliver Maksimovic
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)
,然后简单地比较这两个对象,就好像它们是数字一样。
For example (assuming that $coupon->expire_date
is instance of Carbon
:
例如(假设这$coupon->expire_date
是 的实例Carbon
:
if ($coupon->expire_date >= $my_time) {
// ok
} else {
// error, coupon expired
}
Carbon is very handy for all sorts of comparisons, calculating differences etc. Hereyou can find loads of examples.
Carbon 对于各种比较、计算差异等非常方便。在这里你可以找到大量的例子。
回答by Claudio King
Use date mutatorsin order to make expire_date
field a Carbon instance automatically.
使用date mutators使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']);
}