本文介绍了与数据透视表的Laravel聊天如何使相同的用户返回相同的聊天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个聊天系统,我有三个表(users , chats , user_chat)其中用户聊天是数据透视表,我想每次都检查相同的用户是否想交流回聊天室,而不创建新的聊天室

I am Developing a chat system , I have three tables (users , chats , user_chat) where is the user chat is the pivot table ,I want to check every time that same users want to communicate to back to there chat room and don't create a new chat room

喜欢

这意味着每当**user_id:15****user_id:20**希望进行通信,将它们返回到**chat_room:1**而不创建一个新的方法我已经尝试过array_intersect(),但是没有得到任何结果

thats means that whenever **user_id:15**and **user_id:20** wish to communicate return them to **chat_room:1** and not create a new one how to do it I already tried array_intersect() but i didn't get any result

public function store(Request $requests){
//$requests->user()->id;
$data = $requests->all();
$username1 = User::select('username')->where('id',auth()->guard('api')->user()->id )->first();
$username2 = User::select('username')->where('id',$data['recever_id'] )->first();
$user1Conve[] = Conversation::where('user_id' , '=',auth()->guard('api')->user()->id )->select('chat_room_id')->get();
$user2Conve[] = Conversation::where('user_id' , '=',$data['recever_id'] )->select('chat_room_id')->get();
$result = array_intersect($user2Conve, $user1Conve);
if (count($result) == 0) {

    $chatroom = ChatRoom::create([
        'room_name' => 'محادثة بين' . $username1->username . ' و ' . $username2->username . '', 'sender_id' => auth()->guard('api')->user()->id

    ]);
    $lastId = (int)$chatroom->id;

    Conversation::create(['chat_room_id' => $lastId, 'user_id' => auth()->guard('api')->user()->id]);
    Conversation::create(['chat_room_id' => $lastId, 'user_id' => $data['recever_id']]);

    $message = Messages::create(
        ['message' => $data['message'],
            'user_room_id' => $lastId,
            'user_id' => auth()->guard('api')->user()->id,
        ]);
    $success['chat_room_id'] = $chatroom->id;
    $success['arrayIntersct'] = $result;
    $pusher = new Pusher("418914066f12eac2d5fd", "6e1b5e98b06d7d3ebd7a", "449820", array('cluster' => 'ap2'));
    $pusher->trigger('my-channel', 'my-event', array('message' => $message->message));
}
else {

}

  return response()->json(['code'=>'success','success'=>$success], $this->successStatus);

}

推荐答案

array_intersect检索array1中所有值都存在于第二个数组中的值,因此,我认为您也应该将它与反数组一起使用:

array_intersect serches for all of the values in array1 whose values exist in array two, So I think you should use it with inversed arrays too:

$result1 = array_intersect($user1Conve,$user2Conve);
$result2 = array_intersect($user2Conve, $user1Conve);
if (count($result1) == 0 && count($result2) == 0) {
......

这篇关于与数据透视表的Laravel聊天如何使相同的用户返回相同的聊天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 15:19