Laravel 广播通知 - 尝试发送通知时出错 "array_key_exists()"
Laravel Broadcast notification - Error "array_key_exists()" when try to send notification
我正在尝试在数据更改时向用户发送广播通知。但是每次我尝试发送通知时,都会收到此错误:
array_key_exists(): The first argument should be either a string or an integer
.
这是我的通知:
class MyNotification extends Notification
{
use Queueable;
public $games;
public $title;
public $summary;
public $url;
public function __construct($games)
{
$this->games = $games;
$this->title = "Your games";
$this->summary = "Games for {$games[0]->team}";
$this->url = "/some/url";
}
public function via($notifiable)
{
return ['mail','broadcast','database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject($this->title)
->line($this->summary);
}
/**
* Get the voice representation of the notification.
*
* @param mixed $notifiable
* @return PushMessage
*/
public function toPush($notifiable)
{
// ...
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage($this->toArray($notifiable));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'icon' => 'fa fa-calendar',
'text' => $this->summary,
'title' => $this->title,
'url' => $this->url
];
}
}
当我从 via
数组中删除 'broadcast' 时,一切正常。我不知道我做错了什么!有人可以帮忙吗?谢谢!
编辑
这是我完整的堆栈跟踪
Stack trace:
1. ErrorException->() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:321
2. array_key_exists() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:321
3. Illuminate\Database\Eloquent\Model->getAttribute() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1396
4. Illuminate\Database\Eloquent\Model->getKey() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1406
5. Illuminate\Database\Eloquent\Model->getQueueableId() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
6. Illuminate\Support\HigherOrderCollectionProxy->Illuminate\Support\{closure}() [internal]:0
7. array_map() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Collection.php:638
8. Illuminate\Support\Collection->map() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:254
9. Illuminate\Database\Eloquent\Collection->map() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:61
10. Illuminate\Support\HigherOrderCollectionProxy->__call() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:549
11. Illuminate\Database\Eloquent\Collection->getQueueableIds() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php:25
12. Illuminate\Notifications\Notification->getSerializedPropertyValue() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php:23
13. Illuminate\Notifications\Notification->__sleep() [internal]:0
14. serialize() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:139
15. Illuminate\Queue\Queue->createObjectPayload() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:110
16. Illuminate\Queue\Queue->createPayloadArray() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:88
17. Illuminate\Queue\Queue->createPayload() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:40
18. Illuminate\Queue\SyncQueue->push() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:44
19. Illuminate\Queue\Queue->pushOn() /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php:127
20. Illuminate\Broadcasting\BroadcastManager->queue() /var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:267
21. Illuminate\Events\Dispatcher->broadcastEvent() /var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:190
22. Illuminate\Events\Dispatcher->dispatch() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/Channels/BroadcastChannel.php:51
23. Illuminate\Notifications\Channels\BroadcastChannel->send() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:146
24. Illuminate\Notifications\NotificationSender->sendToNotifiable() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:105
25. Illuminate\Notifications\NotificationSender->Illuminate\Notifications\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php:19
26. Illuminate\Notifications\NotificationSender->withLocale() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:107
27. Illuminate\Notifications\NotificationSender->sendNow() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:79
28. Illuminate\Notifications\NotificationSender->send() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/ChannelManager.php:39
29. Illuminate\Notifications\ChannelManager->send() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
30. Illuminate\Support\Facades\Facade->__callStatic() /var/www/html/app/Http/Controllers/Basketball/BasketballGameController.php:458
31. App\Http\Controllers\Basketball\BasketballGameController->updateJudges() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
32. call_user_func_array() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
33. Illuminate\Routing\Controller->callAction() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
34. Illuminate\Routing\ControllerDispatcher->dispatch() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:219
35. Illuminate\Routing\Route->runController() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:176
36. Illuminate\Routing\Route->run() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:681
37. Illuminate\Routing\Router->Illuminate\Routing\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:130
38. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:43
39. Illuminate\Auth\Middleware\Authenticate->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
40. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/barryvdh/laravel-cors/src/HandleCors.php:58
41. Barryvdh\Cors\HandleCors->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
42. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
43. Illuminate\Routing\Middleware\SubstituteBindings->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
44. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:59
45. Illuminate\Routing\Middleware\ThrottleRequests->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
46. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:105
47. Illuminate\Pipeline\Pipeline->then() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:683
48. Illuminate\Routing\Router->runRouteWithinStack() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658
49. Illuminate\Routing\Router->runRoute() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:624
50. Illuminate\Routing\Router->dispatchToRoute() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:613
51. Illuminate\Routing\Router->dispatch() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:170
52. Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:130
53. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/fideloper/proxy/src/TrustProxies.php:57
54. Fideloper\Proxy\TrustProxies->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
55. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
56. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
57. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
58. Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
59. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:62
60. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
61. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/barryvdh/laravel-cors/src/HandleCors.php:58
62. Barryvdh\Cors\HandleCors->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
63. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:105
64. Illuminate\Pipeline\Pipeline->then() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:145
65. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:110
66. Illuminate\Foundation\Http\Kernel->handle() /var/www/html/public/index.php:55
编辑 2
在我的通知构造函数中,我有一个使用复合键的模型。一旦我删除此行:$this->games = $games;
,并将 $this->summary
编辑为某个字符串 - 它有效! $games
是模型Game
的集合。
但是我不知道为什么会这样?!
如果我理解正确的话,原因很简单,因为你的模型使用了一个复合键 not supported by eloquent。
所以这就是我认为在整个 notification/broadcasting 模块上我的有限知识实际发生的事情。
- 您尝试发送您的通知,然后调度员将您的通知传递给广播管理器,因为您指定在您的 via 方法中广播它
- 然后广播管理员将尝试将您的
MyNotification
事件推入队列
- 为了将事件推送到队列中,它将尝试创建您的通知的有效载荷 class 并尝试将其序列化
- 您要扩展的
Notification
基础 class 实际上实现了 Illuminate\Queue\SerializesModels
特性,因此会自动尝试序列化通知中的所有模型 class' s 属性
- 在尝试序列化
Game
对象的 eloquent 集合时,该集合随后尝试对集合中的每个模型调用 getQueueableId
,但随后失败,因为它尝试根据您的组合键检索属性
至于解决方案,我会说尝试删除模型上的复合键,因为在使用 eloquent.
时它似乎无法按预期工作
// in Game model
protected $primaryKey = 'id';
根据这个 documentation on queues,你也可以尝试在你的 MyNotification
Class 上添加一个 broadcastWith
方法,虽然我不确定这是否将绕过 SerializeModels
trait.
的序列化方法
// in MyNotification
public function broadcastWith()
{
return ['id' => $this->id];
}
或者根据您自己的试验和错误,您可以将其转换为数组,而不是使用集合初始化 $this->games
。毕竟,最终,你的toBroadcast
方法才是真正控制广播的方法。
$this->games = $games->toArray();
您最后的选择是覆盖 Game
模型的 getQueueableId()
方法。
// in Game Model
public function getQueueableId()
{
return $this->id;
// or if you still want to utilize your composite keys. UNTESTED
return $this->key_one.$this->key_two;
}
我知道我来晚了一点,但我经常使用 Laravel/Eloquent 复合键。
并不是说它根本就是一个 exhaustive/complete 存储库,但我确实维护了一个 Git repository 来帮助我处理 Eloquent 的一些缺点(以及添加一些附加功能)。
特别是,我创建了一个特性来覆盖 Eloquent 的一些函数,这些函数的存在专门用于处理 Laravel 顽固地否认 RDBMS 不能全部建立在假 ID
主键字段之上。
我不会在这里以完整形式编写所有覆盖函数,因为它们往往会随着时间的推移而演变,因为我会发现我需要适应的新情况。尽管如此,我目前在我的特征中覆盖的函数名称如下:
public function getKeyName();
public function getKey();
public function setKeysForSaveQuery();
public function getKeyForSaveQuery();
在我的测试中,我的特性确实修复了 OP 报告的(我认为是)错误。
如果有人有兴趣帮助维护这样的存储库,请告诉我!合作总是值得赞赏的。
我正在尝试在数据更改时向用户发送广播通知。但是每次我尝试发送通知时,都会收到此错误:
array_key_exists(): The first argument should be either a string or an integer
.
这是我的通知:
class MyNotification extends Notification
{
use Queueable;
public $games;
public $title;
public $summary;
public $url;
public function __construct($games)
{
$this->games = $games;
$this->title = "Your games";
$this->summary = "Games for {$games[0]->team}";
$this->url = "/some/url";
}
public function via($notifiable)
{
return ['mail','broadcast','database'];
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)
->subject($this->title)
->line($this->summary);
}
/**
* Get the voice representation of the notification.
*
* @param mixed $notifiable
* @return PushMessage
*/
public function toPush($notifiable)
{
// ...
}
public function toBroadcast($notifiable)
{
return new BroadcastMessage($this->toArray($notifiable));
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'icon' => 'fa fa-calendar',
'text' => $this->summary,
'title' => $this->title,
'url' => $this->url
];
}
}
当我从 via
数组中删除 'broadcast' 时,一切正常。我不知道我做错了什么!有人可以帮忙吗?谢谢!
编辑 这是我完整的堆栈跟踪
Stack trace:
1. ErrorException->() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:321
2. array_key_exists() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:321
3. Illuminate\Database\Eloquent\Model->getAttribute() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1396
4. Illuminate\Database\Eloquent\Model->getKey() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1406
5. Illuminate\Database\Eloquent\Model->getQueueableId() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:60
6. Illuminate\Support\HigherOrderCollectionProxy->Illuminate\Support\{closure}() [internal]:0
7. array_map() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Collection.php:638
8. Illuminate\Support\Collection->map() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:254
9. Illuminate\Database\Eloquent\Collection->map() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/HigherOrderCollectionProxy.php:61
10. Illuminate\Support\HigherOrderCollectionProxy->__call() /var/www/html/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Collection.php:549
11. Illuminate\Database\Eloquent\Collection->getQueueableIds() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php:25
12. Illuminate\Notifications\Notification->getSerializedPropertyValue() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SerializesModels.php:23
13. Illuminate\Notifications\Notification->__sleep() [internal]:0
14. serialize() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:139
15. Illuminate\Queue\Queue->createObjectPayload() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:110
16. Illuminate\Queue\Queue->createPayloadArray() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:88
17. Illuminate\Queue\Queue->createPayload() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/SyncQueue.php:40
18. Illuminate\Queue\SyncQueue->push() /var/www/html/vendor/laravel/framework/src/Illuminate/Queue/Queue.php:44
19. Illuminate\Queue\Queue->pushOn() /var/www/html/vendor/laravel/framework/src/Illuminate/Broadcasting/BroadcastManager.php:127
20. Illuminate\Broadcasting\BroadcastManager->queue() /var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:267
21. Illuminate\Events\Dispatcher->broadcastEvent() /var/www/html/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:190
22. Illuminate\Events\Dispatcher->dispatch() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/Channels/BroadcastChannel.php:51
23. Illuminate\Notifications\Channels\BroadcastChannel->send() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:146
24. Illuminate\Notifications\NotificationSender->sendToNotifiable() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:105
25. Illuminate\Notifications\NotificationSender->Illuminate\Notifications\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Traits/Localizable.php:19
26. Illuminate\Notifications\NotificationSender->withLocale() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:107
27. Illuminate\Notifications\NotificationSender->sendNow() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/NotificationSender.php:79
28. Illuminate\Notifications\NotificationSender->send() /var/www/html/vendor/laravel/framework/src/Illuminate/Notifications/ChannelManager.php:39
29. Illuminate\Notifications\ChannelManager->send() /var/www/html/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:261
30. Illuminate\Support\Facades\Facade->__callStatic() /var/www/html/app/Http/Controllers/Basketball/BasketballGameController.php:458
31. App\Http\Controllers\Basketball\BasketballGameController->updateJudges() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
32. call_user_func_array() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:54
33. Illuminate\Routing\Controller->callAction() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:45
34. Illuminate\Routing\ControllerDispatcher->dispatch() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:219
35. Illuminate\Routing\Route->runController() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Route.php:176
36. Illuminate\Routing\Route->run() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:681
37. Illuminate\Routing\Router->Illuminate\Routing\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:130
38. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php:43
39. Illuminate\Auth\Middleware\Authenticate->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
40. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/barryvdh/laravel-cors/src/HandleCors.php:58
41. Barryvdh\Cors\HandleCors->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
42. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
43. Illuminate\Routing\Middleware\SubstituteBindings->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
44. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php:59
45. Illuminate\Routing\Middleware\ThrottleRequests->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
46. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:105
47. Illuminate\Pipeline\Pipeline->then() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:683
48. Illuminate\Routing\Router->runRouteWithinStack() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:658
49. Illuminate\Routing\Router->runRoute() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:624
50. Illuminate\Routing\Router->dispatchToRoute() /var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Router.php:613
51. Illuminate\Routing\Router->dispatch() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:170
52. Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:130
53. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/fideloper/proxy/src/TrustProxies.php:57
54. Fideloper\Proxy\TrustProxies->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
55. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php:21
56. Illuminate\Foundation\Http\Middleware\TransformsRequest->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
57. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php:27
58. Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
59. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php:62
60. Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
61. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/barryvdh/laravel-cors/src/HandleCors.php:58
62. Barryvdh\Cors\HandleCors->handle() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:171
63. Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}() /var/www/html/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:105
64. Illuminate\Pipeline\Pipeline->then() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:145
65. Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter() /var/www/html/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:110
66. Illuminate\Foundation\Http\Kernel->handle() /var/www/html/public/index.php:55
编辑 2
在我的通知构造函数中,我有一个使用复合键的模型。一旦我删除此行:$this->games = $games;
,并将 $this->summary
编辑为某个字符串 - 它有效! $games
是模型Game
的集合。
但是我不知道为什么会这样?!
如果我理解正确的话,原因很简单,因为你的模型使用了一个复合键 not supported by eloquent。
所以这就是我认为在整个 notification/broadcasting 模块上我的有限知识实际发生的事情。
- 您尝试发送您的通知,然后调度员将您的通知传递给广播管理器,因为您指定在您的 via 方法中广播它
- 然后广播管理员将尝试将您的
MyNotification
事件推入队列 - 为了将事件推送到队列中,它将尝试创建您的通知的有效载荷 class 并尝试将其序列化
- 您要扩展的
Notification
基础 class 实际上实现了Illuminate\Queue\SerializesModels
特性,因此会自动尝试序列化通知中的所有模型 class' s 属性 - 在尝试序列化
Game
对象的 eloquent 集合时,该集合随后尝试对集合中的每个模型调用getQueueableId
,但随后失败,因为它尝试根据您的组合键检索属性
至于解决方案,我会说尝试删除模型上的复合键,因为在使用 eloquent.
时它似乎无法按预期工作// in Game model
protected $primaryKey = 'id';
根据这个 documentation on queues,你也可以尝试在你的 MyNotification
Class 上添加一个 broadcastWith
方法,虽然我不确定这是否将绕过 SerializeModels
trait.
// in MyNotification
public function broadcastWith()
{
return ['id' => $this->id];
}
或者根据您自己的试验和错误,您可以将其转换为数组,而不是使用集合初始化 $this->games
。毕竟,最终,你的toBroadcast
方法才是真正控制广播的方法。
$this->games = $games->toArray();
您最后的选择是覆盖 Game
模型的 getQueueableId()
方法。
// in Game Model
public function getQueueableId()
{
return $this->id;
// or if you still want to utilize your composite keys. UNTESTED
return $this->key_one.$this->key_two;
}
我知道我来晚了一点,但我经常使用 Laravel/Eloquent 复合键。
并不是说它根本就是一个 exhaustive/complete 存储库,但我确实维护了一个 Git repository 来帮助我处理 Eloquent 的一些缺点(以及添加一些附加功能)。
特别是,我创建了一个特性来覆盖 Eloquent 的一些函数,这些函数的存在专门用于处理 Laravel 顽固地否认 RDBMS 不能全部建立在假 ID
主键字段之上。
我不会在这里以完整形式编写所有覆盖函数,因为它们往往会随着时间的推移而演变,因为我会发现我需要适应的新情况。尽管如此,我目前在我的特征中覆盖的函数名称如下:
public function getKeyName();
public function getKey();
public function setKeysForSaveQuery();
public function getKeyForSaveQuery();
在我的测试中,我的特性确实修复了 OP 报告的(我认为是)错误。
如果有人有兴趣帮助维护这样的存储库,请告诉我!合作总是值得赞赏的。