attach($user->id) 不工作并且呈现为 null
attach($user->id) isn't working and is rendering as null
我有一个消息系统,允许用户互相发送消息、回复消息以及在收到消息时收到通知。我遇到的问题是 $conversation->participants()->attach($reciever->id);
returns null 并且似乎没有附加对话参与者我 dd()
的价值。
InboxTest.php
public function a_user_can_leave_a_reply_in_conversation()
{
$this->withoutExceptionHandling();
$this->actingAs($user = factory('App\User')->create());
$conversation = factory('App\Conversation')->create();
$reciever = factory('App\User')->create();
$conversation->participants()->attach($reciever->id);
$response = $this->json('POST', '/api/message/'. $conversation->hashed_id . '/reply', ['body'=> 'A new message.'])
->assertStatus(201);
$response->assertJsonFragment(['body' => 'A new message.']);
$this->assertDatabaseHas('messages', [
'conversation_id' => $conversation->id,
'sender_id' => $user->id,
'body' => 'A new message.'
]);
$this->assertDatabaseHas('conversation_participants',[
'user_id' => $reciever->id
]);
}
这也导致通知系统出错:
Error: Call to a member function routeNotificationFor() on null
InboxController.php
public function reply($hashedId, Request $request)
{
$this->validate($request, [
'body' => 'required',
]);
$conversation = Conversation::where('hashed_id', $hashedId)->first();
$users = $conversation->recipients;
$notifications = Notification::send($users, new MessageNotification(auth()->user()));
$message = $conversation->messages()->create([
'sender_id' => auth()->user()->id,
'body' => $request->body,
]);
return new MessageResource($message);
}
这是我的人际关系:
Conversations.php
public function participants()
{
return $this->belongsToMany('App\User' ,'conversation_participants','conversation_id','user_id')->withPivot(['status','is_sender']);
}
User.php
public function conversations()
{
return $this->belongsToMany('App\Conversation','conversation_participants', 'user_id', 'conversation_id');
}
participants()
关系方法中的 withPivot()
有没有可能搞砸了?我做错了什么?
我在对话模型中没有看到任何 recipients
关系。
但是在控制器中,您试图让用户使用收件人关系,并将通知发送给这些用户。
InboxController.php
$users = $conversation->recipients;
$notifications = Notification::send($users, new
MessageNotification(auth()->user()));
我从您的测试功能中了解到,您没有将任何用户附加到收件人方法。您将其附加到参与者关系中。
InboxTest.php
$reciever = factory('App\User')->create();
$conversation->participants()->attach($reciever->id);
如您所写的那样,附加方式非常好。
也许与participants/recipients关系有关。如果我有任何错误,请在评论中告诉我。
我有一个消息系统,允许用户互相发送消息、回复消息以及在收到消息时收到通知。我遇到的问题是 $conversation->participants()->attach($reciever->id);
returns null 并且似乎没有附加对话参与者我 dd()
的价值。
InboxTest.php
public function a_user_can_leave_a_reply_in_conversation()
{
$this->withoutExceptionHandling();
$this->actingAs($user = factory('App\User')->create());
$conversation = factory('App\Conversation')->create();
$reciever = factory('App\User')->create();
$conversation->participants()->attach($reciever->id);
$response = $this->json('POST', '/api/message/'. $conversation->hashed_id . '/reply', ['body'=> 'A new message.'])
->assertStatus(201);
$response->assertJsonFragment(['body' => 'A new message.']);
$this->assertDatabaseHas('messages', [
'conversation_id' => $conversation->id,
'sender_id' => $user->id,
'body' => 'A new message.'
]);
$this->assertDatabaseHas('conversation_participants',[
'user_id' => $reciever->id
]);
}
这也导致通知系统出错:
Error: Call to a member function routeNotificationFor() on null
InboxController.php
public function reply($hashedId, Request $request)
{
$this->validate($request, [
'body' => 'required',
]);
$conversation = Conversation::where('hashed_id', $hashedId)->first();
$users = $conversation->recipients;
$notifications = Notification::send($users, new MessageNotification(auth()->user()));
$message = $conversation->messages()->create([
'sender_id' => auth()->user()->id,
'body' => $request->body,
]);
return new MessageResource($message);
}
这是我的人际关系:
Conversations.php
public function participants()
{
return $this->belongsToMany('App\User' ,'conversation_participants','conversation_id','user_id')->withPivot(['status','is_sender']);
}
User.php
public function conversations()
{
return $this->belongsToMany('App\Conversation','conversation_participants', 'user_id', 'conversation_id');
}
participants()
关系方法中的 withPivot()
有没有可能搞砸了?我做错了什么?
我在对话模型中没有看到任何 recipients
关系。
但是在控制器中,您试图让用户使用收件人关系,并将通知发送给这些用户。
InboxController.php
$users = $conversation->recipients;
$notifications = Notification::send($users, new
MessageNotification(auth()->user()));
我从您的测试功能中了解到,您没有将任何用户附加到收件人方法。您将其附加到参与者关系中。
InboxTest.php
$reciever = factory('App\User')->create();
$conversation->participants()->attach($reciever->id);
如您所写的那样,附加方式非常好。
也许与participants/recipients关系有关。如果我有任何错误,请在评论中告诉我。