Php 致命错误:在null上调用成员函数unique()
当我尝试应用优惠券时,我遇到了这个错误,我不是脚本的创建者,我只是在使用它,我需要帮助来运行它 这就是代码Php 致命错误:在null上调用成员函数unique(),php,html,laravel,tags,Php,Html,Laravel,Tags,当我尝试应用优惠券时,我遇到了这个错误,我不是脚本的创建者,我只是在使用它,我需要帮助来运行它 这就是代码 private function validate_coupon_from_db($parts){ $parts = $parts->unique('code'); $model_codes = $parts->pluck('code')->all(); $existing_part = Part::whereIn
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,
];
}
}
}
}
private函数验证来自\u db的\u优惠券($parts){
$parts=$parts->unique('code');
$model_code=$parts->pull('code')->all();
$existing_part=part::where('code',$model_code)->where('date','!=','0000-00-00 00:00:00')->orderBy('created_at','desc')->groupBy('code')->get();//->where('date','>',\Carbon Carbon now()->addHour(2))->get();
如果($existing_part->count()<$parts->count()){
返回[
“状态”=>100,
];
}否则{
//$part_not_valid=$existing_part->where('date','首先返回函数validate_-touction_from_db()中第一行之后的$parts,以检查$parts是否有一些值或为空
我认为它是null,所以不能在null上运行unique(),只能在对象上运行unique()。在从\u db()调用验证\u优惠券之前,$parts变量正在提交nullCheck$parts
。显然,您发送的是null,而不是它期望的集合对象。我如何才能做到这一点?您是否可以按照我的要求重写代码