【发布时间】:2016-09-26 20:32:25
【问题描述】:
当我尝试申请优惠券时,我收到了这个错误,我不是脚本的创建者,我只是在使用它,我需要帮助才能运行它
这是代码
private function validate_coupon_from_db($parts){
$parts = $parts->unique('code');
$model_codes = $parts->pluck('code')->all();
$existing_part = Part::whereIn('code',$model_codes)->where('date', '!=', '0000-00-00 00:00:00')->orderBy('created_at', 'desc')->groupBy('code')->get();//->where('date', '>', \Carbon\Carbon::now()->addHour(2))->get();
if($existing_part->count() < $parts->count()){
return [
'status' => 100,
];
}else{
//$part_not_valid = $existing_part->where('date', '<=', \Carbon\Carbon::now()->addHour(2))->all();
$part_not_valid = $existing_part->filter(function ($item) {
return strtotime($item->date) < strtotime(date("Y-m-d H:i:s", strtotime('+2 hours')));
});
if($part_not_valid->count() > 0){
$part_not_valid =$part_not_valid->all();
$string = '';
foreach ($part_not_valid as $part){
$string .= '<br><hr><span> '.$part->code .' '. $part->part_one_name .' - '. $part->part_two_name .' </span>';
}
return [
'status' => 0,
'data' =>
'<div id="s_w_PC_cCoupon_pnlMsgIns" class="divCouponError" style="background-image:url(//static.planetwin365.com/App_Themes/PlanetWin365/Images/ErrorTypes/Errors.png);">'.
'<span id="s_w_PC_cCoupon_errMsgIns"><span>Les côtes suivantes ne sont plus disponibles:</span>'. $string .'</div>'
];
}
else{
return [
'status' => 1,
];
}
}
}
}
这是我遇到的错误
错误: Symfony\Compo nent\Debug\Exception\FatalThrowableError:致命错误:调用成员函数 唯一()为空
【问题讨论】:
-
$parts 变量为空
-
在致电
validate_coupon_from_db()之前检查$parts。显然,您发送的是 null 而不是它所期望的 Collection 对象。 -
我该怎么做,你能按照我应该的方式重写代码