2016-08-10 6 views
1

現在、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>

+0

あなたはjsfiddleを作成できますか? jsFiddleの –

+0

は、PHPコードを実行できません。しかし、私はちょうどそこにhtmlとjavascriptコードを配置..... https://jsfiddle.net/twcwfy2w/ – tisuchi

+0

リアルタイムチャットを統合するためのより良いアプローチはXMPPです。 それ以外の場合は、db内の更新をチェックし、より短い時間間隔でビューを変更するスクリプトを作成する必要があります。 –

答えて

0

私は、ユーザー画面でアペンドフォーマットを更新してIAMが、あなたの出力データについては適合ではありません。

$.ajax({ 
type: 'GET', 
url: takeHash, 
success: function(response) { 
appenddata='<li>'; 
    if (typeof(response)=='array') { 
    appenddata+=response['message']; 
    } 
    if (typeof(response)=='object') { 
    appenddata+=response->message; 
    } 
    if (typeof(response)=='string') { 
    appenddata+=response; 
    } 
    appenddata+='</li>'; 
    $('.convo-body').append(appenddata); 

} 
}); 

}, 1000); 
+0

これはどのようにして問題を解決しますか? – Takarii

+0

あなたの答えは...実際に私のメッセージディスプレイのHTMLコードは私の質問の最初のブロックにあります。私はそこに私の戻りデータを表示する必要があります...それは私が解決する必要がある問題です... – tisuchi

+0

ので、jquery foreachを使用 array.forEach(function(currentValue、index、arr)、thisValue) – Joyson

関連する問題