現在、laravelのリアルタイムチャットモジュールで働いています。私は、AJAXによってデータベースにユーザーチャットデータを送信することができますが、リフレッシュせずにデータベースからデータを表示することはできません。ここでページをリフレッシュせずにlaravelデータを取得する方法は?
ここデータベース -
<ul class="convo-list convo-body">
<li>
<div class="row convo-row-msg">
<div class="col-xs-2 convo-row-msg-avatar">
<a href="{{ url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}">
<img src="{{url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}" alt="" width="220px" style="border: 4px solid #fff;border-radius:2px;">
</a>
</div>
<div class="col-xs-10 convo-row-body">
<b><a href="#"> Username place here </a> </b>
<span class="timestamp">
Timestamp
</span>
<p class="convo-content"> User chat message will be there </p>
</div>
</div>
</li>
</ul>
から来たメッセージを表示する場所は、私はそのようなデータベース -
var takeHash = $('#hash').val();
var interval = setInterval(function(){
$.ajax({
type: 'GET',
url: takeHash,
success: function(response) {
$('.convo-body').append(response);
}
});
}, 1000);
最後に私のコントローラの応答から取得する必要があり、私のAJAXですway-
public function showSingleMessage($hash)
{
$msg = Message::where('conversationhash', $hash)
->where('fromid', '!=', Auth::user()->id)
->get();
$fromid = '';
foreach ($msg as $value) {
$fromid = $value->fromid;
}
$messages = Message::where('conversationhash', $hash)
->Where('fromid', Auth::user()->id)
->orWhere('fromid', $fromid)
->get();
$user = new User();
return view('messages.show-single')
->with(['messages' => $messages, 'user' => $user, 'touser' => $fromid]);
}
ブレードテンプレートにデータを表示する方法を教えてください。 Aravelはそれを使用する)?
var takeHash = $('#hash').val();
var interval = setInterval(function(){
$.ajax({
type: 'GET',
url: takeHash,
success: function(response) {
$('.convo-body').append(response);
}
});
}, 1000);
<ul class="convo-list convo-body">
<li>
<div class="row convo-row-msg">
<div class="col-xs-2 convo-row-msg-avatar">
<a href="{{ url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}">
<img src="{{url('/') }}/images/users/photos/profiles/{{ $users->profilephoto }}" alt="" width="220px" style="border: 4px solid #fff;border-radius:2px;">
</a>
</div>
<div class="col-xs-10 convo-row-body">
<b><a href="#"> Username place here </a> </b>
<span class="timestamp">
Timestamp
</span>
<p class="convo-content"> User chat message will be there </p>
</div>
</div>
</li>
</ul>
あなたはjsfiddleを作成できますか? jsFiddleの –
は、PHPコードを実行できません。しかし、私はちょうどそこにhtmlとjavascriptコードを配置..... https://jsfiddle.net/twcwfy2w/ – tisuchi
リアルタイムチャットを統合するためのより良いアプローチはXMPPです。 それ以外の場合は、db内の更新をチェックし、より短い時間間隔でビューを変更するスクリプトを作成する必要があります。 –