,可以通过以下步骤实现:
public function receiver()
{
return $this->belongsTo(User::class, 'receiver_id');
}
在控制器中,添加发送消息和接收消息的方法,并定义相应的路由。例如:
// 发送消息
public function sendMessage(Request $request)
{
$message = new Message;
$message->sender_id = auth()->user()->id;
$message->receiver_id = $request->receiver_id;
$message->message = $request->message;
$message->save();
return response()->json(['message' => 'Message sent successfully']);
}
// 接收消息
public function receiveMessage()
{
$user = auth()->user();
$messages = Message::where('receiver_id', $user->id)->with('sender')->get();
return response()->json(['messages' => $messages]);
}
在路由文件中,定义发送消息和接收消息的路由。例如:
Route::post('/send-message', 'MessageController@sendMessage');
Route::get('/receive-message', 'MessageController@receiveMessage');
注意:以上推荐的腾讯云产品仅供参考,具体选择和使用需根据实际需求进行评估和决策。
领取专属 10元无门槛券
手把手带您无忧上云