雄辩的父特质 - 调用未定义的方法

时间:2014-09-08 15:26:06

标签: php laravel laravel-4

Notifiable是多个应通报的Eloquent模型的父trait

我的问题是,当我尝试在sendGCMNotifications特征中调用Notifiable时,我收到以下错误:

"BadMethodCallException","message":"Call to undefined method 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]);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

您在特征中的方法是sendGCMNotification而不是sendGCMNotifications。你错过了s的结尾:)

或者反之亦然 - 致sendGCMNotifications的电话号码s可能不应该存在。

无论哪种方式,您都将其称为sendGCMNotifications,但将其定义为sendGCMNotification