gpt4 book ai didi

php - Eloquent parent trait - 调用未定义的方法

转载 作者:行者123 更新时间:2023-12-05 05:26:29 25 4
gpt4 key购买 nike

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

我的问题是,当我尝试在 Notifiable 特征中调用 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]);
}
});
}
}

最佳答案

特征中的方法是 sendGCMNotification 而不是 sendGCMNotifications。你错过了最后的 s :)

或者反过来 - 对 sendGCMNotifications 的调用有一个可能不应该存在的 s

您可以将其称为 sendGCMNotifications 但将其定义为 sendGCMNotification

关于php - Eloquent parent trait - 调用未定义的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25727718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com