当我尝试在这种情况下使用范围时,请返回以下错误:
Call to undefined method Illuminate\Database\Query\Builder::isPromotionTypeIdScope() (View: C:\MAMP\htdocs\mysite\resources\views\site\home.blade.php)
逻辑是:
如果我用所有子句(来自范围)替换isPromotionTypeIdScope(),可以正常工作,但如果我使用范围会给我错误,有什么建议吗?
关于结构的一些东西不起作用。我在其他模型中使用范围并且没有问题。找不到什么错误。
是否有可能,因为我试图添加范围(例如:->promotion()->isPromotionTypeIdScope($promotion_type_id)
)?
public function product()
{
return $this->belongsTo('App\Models\Product', 'product_id');
}
public function promotion(){
return $this->belongsToMany('App\Models\Promotion', 'promotion_product_prices', 'product_price_id', 'promotion_id');
}
public function single_promotion($promotion_type_id = 0){
return $this->promotion()->isPromotionTypeIdScope($promotion_type_id)->first() ?? false;
}
public function category_promotion($promotion_type_id = 0){
return $this->product()->first()
->category()
->first()
->promotion()
->isPromotionTypeIdScope($promotion_type_id)
->first() ?? false;
}
public function full_promotion($promotion_type_id = 0)
{
return Promotion::where('full', 1)->isPromotionTypeIdScope($promotion_type_id)->first() ?? false;
}
public function hasPromotion($promotion_type_id = 0){
if($this->full_promotion($promotion_type_id) !== false){
return $this->full_promotion($promotion_type_id);
}elseif($this->category_promotion($promotion_type_id) !== false){
return $this->category_promotion($promotion_type_id);
}elseif($this->single_promotion($promotion_type_id) !== false){
return $this->single_promotion($promotion_type_id);
}else{
return false;
}
}
public function scopeIsPromotionTypeIdScope($query, $promotion_type_id=0){
if($promotion_type_id != 0){
return $query->where('promotion_type_id', $promotion_type_id)
->where('validity_from', '<=', date('Y-m-d H:i:s'))
->where('validity_to', '>=', date('Y-m-d H:i:s'))
->where('active', 1)
->orderBy('updated_at', 'DESC')->limit(1);
}else{
return $query;
}
}
答案 0 :(得分:1)
您可以随时在SELECT `TEMPAT LAHIR`,
COUNT(CASE WHEN (PENDIDIKAN = 'S1') THEN PENDIDIKAN ELSE NULL END) AS S1,
COUNT(CASE WHEN (PENDIDIKAN = 'S2') THEN PENDIDIKAN ELSE NULL END) AS S2,
COUNT(CASE WHEN (PENDIDIKAN = 'SMA') THEN PENDIDIKAN ELSE NULL END) AS SMA
FROM `table_name`
GROUP BY `TEMPAT LAHIR`
模型上调用isPromotionTypeIdScope
方法,因此您应该在Promotion
模型中定义scopeIsPromotionTypeIdScope
而不是此模型。