华丽的父级特征 - 调用未定义方法

3

Notifiable是多个可通知的Eloquent模型的父trait

我的问题是,当我尝试在Notifiable trait内调用sendGCMNotifications时,会出现以下错误:

"BadMethodCallException","message":"调用未定义的方法Illuminate\\Database\\Query\\Builder::sendGCMNotifications()"

以下是我的代码:

trait Notifiable {      

    public function notifyUsers($params) {
        $notifications = // some array
        $this->sendGCMNotifications($notifications[0], $receiverIds);
        Notification::insert($notifications);
    }

    public function notifications() {
        return $this->morphMany('Notification', 'notifiable');
    }

    public function notifyCompanyAdmins($params) {
        $receiverIds = Company::with('users')->find($companyId)->users->lists('id');
        $this->notifyUsers(array_merge($params, ['receiverIds' => $receiverIds]));
    }
    public function sendGCMNotification($notification, $receiverIds, $action = NORMAL_NOTIFICATION) {

        foreach ($receiverIds as $id) {
            $registration_ids[] = User::find($id)->gcm_registration_id;
        }

        $response = Curl::post('https://android.googleapis.com/gcm/send', [
            'data' => [
                'action' => $action,
                'message' => $notification['message']
                ],
            'registration_ids' => $registration_ids,
            ]);

    }
}

继承上述特性的位置模型:

class Location extends Eloquent {
    use Confirmable, Notifiable;

    const NOTIFICATION_MESSAGE = 'a location requires confirmation, submitted by #userFullName';
    const NOTIFICATION_TITLE = 'Location needs confirmation.';

    private $createdByAdmin;

    public static function boot() {
        parent::boot();
        Location::saved(function($location) {

            if (!$location->createdByAdmin) {
                $location->notifyCompanyAdmins([
                    'companyId' => Auth::user()->company_id,
                    'title' => self::NOTIFICATION_TITLE,
                    'message' => str_replace('#userFullName', Auth::user()->full_name, self::NOTIFICATION_MESSAGE),
                    'senderId' => Auth::user()->id,
                    ]);
            } else {
                $location->setConfirmed(['confirmed' => true, 'adminId' => Auth::user()->id]);
            }
        });
    }
}
2个回答

2
您在trait中的方法是sendGCMNotification而不是sendGCMNotifications。您遗漏了结尾的s :)
或者反过来 - 调用sendGCMNotifications有一个s,可能不应该出现。
无论哪种方式,您都调用它作为sendGCMNotifications,但定义为sendGCMNotification

1
我的天啊!!我一直在忙其他的事情,这个完全没想到 :) - Walid Ammar

0
也许这会帮助未来的某个人:我遇到了同样的问题,因为我忘记添加了以下内容:
use xxx;

在父类中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接