Laravel 通知以空 user_id 保存在数据库中
Laravel notifications are saved in the database with null user_id
当我尝试为用户保存通知时,user_id 列在数据库中始终为 NULL。
我将 Notifiable
特征分配给 User
模型,当我使用 Notification
外观时也会发生这种情况。它只是不在 user_id 列中保存任何内容。
我通过关系获取用户,例如 $this->buyer->user
,其中 Buyer
模型属于 User
。我可以看到用户已正确检索并具有 ID。
App\Models\User {#1532
#fillable: array:5 [
0 => "name"
1 => "last_name"
2 => "email"
3 => "password"
4 => "account_type"
]
#hidden: array:4 [
0 => "password"
1 => "remember_token"
2 => "two_factor_recovery_codes"
3 => "two_factor_secret"
]
#casts: array:1 [
"email_verified_at" => "datetime"
]
#appends: array:1 [
0 => "profile_photo_url"
]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:14 [
"id" => 20
"name" => "Test"
"last_name" => "User"
"email" => "test _at mail com"
"email_verified_at" => "2021-03-12 18:17:28"
"password" => ""
"two_factor_secret" => null
"two_factor_recovery_codes" => null
"remember_token" => null
"account_type" => "buyer"
"current_team_id" => null
"profile_photo_path" => null
"created_at" => "2021-03-12 18:17:22"
"updated_at" => "2021-03-12 18:17:53"
]
#original: array:14 [
"id" => 20
"name" => "Test"
"last_name" => "User"
"email" => "test _at mail com"
"email_verified_at" => "2021-03-12 18:17:28"
"password" => ""
"two_factor_secret" => null
"two_factor_recovery_codes" => null
"remember_token" => null
"account_type" => "buyer"
"current_team_id" => null
"profile_photo_path" => null
"created_at" => "2021-03-12 18:17:22"
"updated_at" => "2021-03-12 18:17:53"
]
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
#accessToken: null
}
我尝试这样发送通知:
$this->buyer->user->notify(new \App\Notifications\MessageReceived(
$this->message,
'user 2')
);
MessageReceived.php 文件如下所示
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class MessageReceived extends Notification
{
use Queueable;
public $message;
public $sender;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($message, $sender)
{
$this->message = $message;
$this->sender = $sender;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [ 'broadcast', 'database' ];
}
/**
* Get the broadcastable representation of the notification.
*
* @param mixed $notifiable
* @return BroadcastMessage
*/
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'message' => $this->message,
'sender' => $this->sender,
'link' => '/chat/'
]);
}
/**
* Get the type of the notification being broadcast.
*
* @return string
*/
public function broadcastType()
{
return 'broadcast.message';
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)->subject('New message received')->view();
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'message' => 'You have a new message from ' . $this->sender,
'link' => $this->business->id . '/chat/' . $this->channel,
'type' => 'broadcast.message'
];
}
}
当我记录数据库查询时,我得到了这个操作:
array (
'query' => 'insert into `notifications` (`id`, `type`, `data`, `read_at`, `notifiable_id`, `notifiable_type`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?)',
'bindings' =>
array (
0 => '960ec22c-f121-4ef7-acca-c0e6105fd760',
1 => 'App\Notifications\MessageReceived',
2 => '{"message":"You have a new message from user 2","link":"/chat/","type":"broadcast.message"}',
3 => NULL,
4 => 20,
5 => 'App\Models\User',
6 => '2021-05-20 19:37:20',
7 => '2021-05-20 19:37:20',
),
'time' => 148.9,
),
我在这个查询中没有看到 user_id
列。这是一个错误还是我做错了什么?我已经遇到这个问题好几个星期了,找不到罪魁祸首。我使用的是 8.12
版本
用户 ID 或通知 ID 存储在 notifiable_id 列。
您可以使用 $user->notifications 或 $user->unreadNotifications().
之间的关系获取与用户相关的通知
当我尝试为用户保存通知时,user_id 列在数据库中始终为 NULL。
我将 Notifiable
特征分配给 User
模型,当我使用 Notification
外观时也会发生这种情况。它只是不在 user_id 列中保存任何内容。
我通过关系获取用户,例如 $this->buyer->user
,其中 Buyer
模型属于 User
。我可以看到用户已正确检索并具有 ID。
App\Models\User {#1532
#fillable: array:5 [
0 => "name"
1 => "last_name"
2 => "email"
3 => "password"
4 => "account_type"
]
#hidden: array:4 [
0 => "password"
1 => "remember_token"
2 => "two_factor_recovery_codes"
3 => "two_factor_secret"
]
#casts: array:1 [
"email_verified_at" => "datetime"
]
#appends: array:1 [
0 => "profile_photo_url"
]
#connection: "mysql"
#table: "users"
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:14 [
"id" => 20
"name" => "Test"
"last_name" => "User"
"email" => "test _at mail com"
"email_verified_at" => "2021-03-12 18:17:28"
"password" => ""
"two_factor_secret" => null
"two_factor_recovery_codes" => null
"remember_token" => null
"account_type" => "buyer"
"current_team_id" => null
"profile_photo_path" => null
"created_at" => "2021-03-12 18:17:22"
"updated_at" => "2021-03-12 18:17:53"
]
#original: array:14 [
"id" => 20
"name" => "Test"
"last_name" => "User"
"email" => "test _at mail com"
"email_verified_at" => "2021-03-12 18:17:28"
"password" => ""
"two_factor_secret" => null
"two_factor_recovery_codes" => null
"remember_token" => null
"account_type" => "buyer"
"current_team_id" => null
"profile_photo_path" => null
"created_at" => "2021-03-12 18:17:22"
"updated_at" => "2021-03-12 18:17:53"
]
#changes: []
#classCastCache: []
#dates: []
#dateFormat: null
#dispatchesEvents: []
#observables: []
#relations: []
#touches: []
+timestamps: true
#visible: []
#guarded: array:1 [
0 => "*"
]
#rememberTokenName: "remember_token"
#accessToken: null
}
我尝试这样发送通知:
$this->buyer->user->notify(new \App\Notifications\MessageReceived(
$this->message,
'user 2')
);
MessageReceived.php 文件如下所示
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\BroadcastMessage;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class MessageReceived extends Notification
{
use Queueable;
public $message;
public $sender;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct($message, $sender)
{
$this->message = $message;
$this->sender = $sender;
}
/**
* Get the notification's delivery channels.
*
* @param mixed $notifiable
* @return array
*/
public function via($notifiable)
{
return [ 'broadcast', 'database' ];
}
/**
* Get the broadcastable representation of the notification.
*
* @param mixed $notifiable
* @return BroadcastMessage
*/
public function toBroadcast($notifiable)
{
return new BroadcastMessage([
'message' => $this->message,
'sender' => $this->sender,
'link' => '/chat/'
]);
}
/**
* Get the type of the notification being broadcast.
*
* @return string
*/
public function broadcastType()
{
return 'broadcast.message';
}
/**
* Get the mail representation of the notification.
*
* @param mixed $notifiable
* @return \Illuminate\Notifications\Messages\MailMessage
*/
public function toMail($notifiable)
{
return (new MailMessage)->subject('New message received')->view();
}
/**
* Get the array representation of the notification.
*
* @param mixed $notifiable
* @return array
*/
public function toArray($notifiable)
{
return [
'message' => 'You have a new message from ' . $this->sender,
'link' => $this->business->id . '/chat/' . $this->channel,
'type' => 'broadcast.message'
];
}
}
当我记录数据库查询时,我得到了这个操作:
array (
'query' => 'insert into `notifications` (`id`, `type`, `data`, `read_at`, `notifiable_id`, `notifiable_type`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, ?, ?)',
'bindings' =>
array (
0 => '960ec22c-f121-4ef7-acca-c0e6105fd760',
1 => 'App\Notifications\MessageReceived',
2 => '{"message":"You have a new message from user 2","link":"/chat/","type":"broadcast.message"}',
3 => NULL,
4 => 20,
5 => 'App\Models\User',
6 => '2021-05-20 19:37:20',
7 => '2021-05-20 19:37:20',
),
'time' => 148.9,
),
我在这个查询中没有看到 user_id
列。这是一个错误还是我做错了什么?我已经遇到这个问题好几个星期了,找不到罪魁祸首。我使用的是 8.12
用户 ID 或通知 ID 存储在 notifiable_id 列。 您可以使用 $user->notifications 或 $user->unreadNotifications().
之间的关系获取与用户相关的通知