我正在尝试获取所有用户通知,具体取决于用户是买家还是卖家(可以同时是两者)。我在通知表中创建了两个函数来相互过滤。我的目标是最终运行:
$notifications = Auth::user()->notifications()->getBuyerNotifications();
或者
$notifications = Auth::user()->notifications()->getSellerNotifications();
我遇到了一个问题:Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany
用户模型:
public function notifications() {
return $this->hasMany('App\Notification', 'user_id', 'id');
}
通知模型:
public function user() {
return $this->belongsTo('App\User', 'id', 'user_id');
}
public static function getBuyerNotifications() {
return self::whereNotNull('buyer_id')
->whereNull('deleted_at')
->get();
}
public static function getSellerNotifications() {
return $this->whereNotNull('seller_id')
->whereNull('deleted_at')
->get();
}
如果他们是买家,我想运行该命令来获取所有用户通知:$notifications = Auth::user()->notifications()->getBuyerNotifications();