我正在尝试在 Laravel 中为我的网站创建聊天。我正在为用户选择最近的聊天记录,用户可以在其中看到可以与其聊天的用户列表。
这是我的询问:
$data = [];
$chatusers = Chat::latest()->get(['reciver_id']);
$chatusers = collect($chatusers)->unique()->values()->all();
foreach ($chatusers as $user) {
if ($user['reciver_id'] >= 5000) {
$message = Chat::where('reciver_id', $user['reciver_id'])
->latest()
->first();
array_push($data, $message);
} else {
$message = Chat::where('sender_id', $user['reciver_id'])
->orWhere('reciver_id', $user['reciver_id'])
->latest()
->first();
array_push($data, $message);
}
}
这里我附上数据库表的截图: https://i.sstatic.net/dKEKs.jpg
这是聊天列表的屏幕:- https://i.sstatic.net/6VAmH.jpg
我不是在管理关系。
$login_user_id = 1; // use auth user_id
$chat = Chat::where(function($q) use($login_user_id){
$q->where('sender_id',$login_user_id)>orWhere('receiver_id',$login_user_id);
$q->where('receiver_id',$login_user_id)->orWhere('sender_id',$login_user_id);
})->orderBy('created_at','desc')->get()->toArray();
$other_user_id = [];
$message = [];
foreach ($chat as $key => $value) {
if(in_array($value->sender_id, $other_user_id) || in_array($value->receiver_id, $other_user_id)){
continue;
}
if($value->sender_id != $login_user_id){
$other_user_id[] = $value->sender_id;
}else{
$other_user_id[] = $value->receiver_id;
}
$message[] = $value;
}
print_r($message);
您必须添加表格来管理未读消息。