在 onOpen 函数中返回一个数组
Returning an array in onOpen function
我目前正在编写一个聊天应用程序。到目前为止,用户可以互相发送消息,我可以将消息存储在我的数据库中。现在,当用户打开页面向另一个用户发送消息时,我想在我的 onOpen 函数中显示存储在数据库中的消息。
这是我的 onOpen 函数的样子:
public function onOpen(ConnectionInterface $conn)
{
$query = $conn->httpRequest->getUri()->getQuery();
preg_match_all('!\d+!', $query, $matches);
$my_id = $matches[0][0];
$friend_id = $matches[0][1];
$conn->resourceId = $my_id;
$this->users[$conn->resourceId] = $conn;
$messages = Private_message::where([
['user1',$my_id],
['user2',$friend_id]
])->orWhere([
['user1',$friend_id],
['user2',$my_id]
])->get()->toArray();
}
我想 return $messages
数组到我的 privateChat.blade.php
视图,这样我就可以将它们打印出来。
我尝试在我的 privateChat.blade.php
中执行 return view('privateChat',['messages' => $messages]);
和 @dd($messages)
,但它说变量未定义。
我还尝试了 return $messages
并在我的 onopen js 函数中将它们打印出来,但它不起作用。正确的方法是什么?
如您所见,您没有返回任何内容以供查看
所以
public function onOpen(ConnectionInterface $conn)
{
$query = $conn->httpRequest->getUri()->getQuery();
preg_match_all('!\d+!', $query, $matches);
$my_id = $matches[0][0];
$friend_id = $matches[0][1];
$conn->resourceId = $my_id;
$this->users[$conn->resourceId] = $conn;
$messages = Private_message::where([
['user1',$my_id],
['user2',$friend_id]
])->orWhere([
['user1',$friend_id],
['user2',$my_id]
])->get()->toArray();
$viewShareVariables = compact('messages');
return view('privateChat',$viewShareVariables);
}
如有任何问题欢迎在下方评论
我目前正在编写一个聊天应用程序。到目前为止,用户可以互相发送消息,我可以将消息存储在我的数据库中。现在,当用户打开页面向另一个用户发送消息时,我想在我的 onOpen 函数中显示存储在数据库中的消息。
这是我的 onOpen 函数的样子:
public function onOpen(ConnectionInterface $conn)
{
$query = $conn->httpRequest->getUri()->getQuery();
preg_match_all('!\d+!', $query, $matches);
$my_id = $matches[0][0];
$friend_id = $matches[0][1];
$conn->resourceId = $my_id;
$this->users[$conn->resourceId] = $conn;
$messages = Private_message::where([
['user1',$my_id],
['user2',$friend_id]
])->orWhere([
['user1',$friend_id],
['user2',$my_id]
])->get()->toArray();
}
我想 return $messages
数组到我的 privateChat.blade.php
视图,这样我就可以将它们打印出来。
我尝试在我的 privateChat.blade.php
中执行 return view('privateChat',['messages' => $messages]);
和 @dd($messages)
,但它说变量未定义。
我还尝试了 return $messages
并在我的 onopen js 函数中将它们打印出来,但它不起作用。正确的方法是什么?
如您所见,您没有返回任何内容以供查看
所以
public function onOpen(ConnectionInterface $conn)
{
$query = $conn->httpRequest->getUri()->getQuery();
preg_match_all('!\d+!', $query, $matches);
$my_id = $matches[0][0];
$friend_id = $matches[0][1];
$conn->resourceId = $my_id;
$this->users[$conn->resourceId] = $conn;
$messages = Private_message::where([
['user1',$my_id],
['user2',$friend_id]
])->orWhere([
['user1',$friend_id],
['user2',$my_id]
])->get()->toArray();
$viewShareVariables = compact('messages');
return view('privateChat',$viewShareVariables);
}
如有任何问题欢迎在下方评论