Laravel 7-发送ajax请求以获取收件箱中特定联系人的最新(n)条消息,并将其呈现到页面获取的_messages中

莫斯塔法哈纳

JSON数据需要在fetched_messages上呈现

ChatController @方法“ fetchMessages”

/**
 * Fetch all messages related to specific contact
 *
 * @return Message
 */
public function fetchMessages($id)
{

    $messages = DB::table('messages')
       ->where(function ($query) use($id) {
           $query->where('sender_id', Auth::user()->id)
                 ->where('receiver_id', $id);
       })
       ->orWhere(function ($query) use($id) {
           $query->where('sender_id', $id)
                 ->where('receiver_id', Auth::user()->id);
       })
       ->latest()
       ->paginate(20);
    return Response::json($messages);
  
}

Ajax用于将ID从刀片服务器获取到ChatController

$(document).ready(function(){

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });
    $('.tab-next').click(function(e){
        e.preventDefault();
        let index = $(this);
        let userId = $(this).attr('user-id');
        $.ajax({
            type:'GET',
            url:'/messages/fetch/' + userId,
            success:function(response){
                console.log(response);
   
            }
        });
    });
    
});

在web.php上的聊天路线

Route::get('messages', 'ChatsController@index')->name('msg');
Route::get('messages/fetch/{id}', 'ChatsController@fetchMessages')->name('msg.fetch');
Route::get('messages/post', 'ChatsController@sendMessage')->name('msg.post');

任何帮助,将不胜感激。提前致谢

莫斯塔法哈纳

我为此付出了很大的努力,但是猜猜是什么?我提出的解决方案是通过javascript和HTML附加在json中返回的数据响应。任何人都有类似的任务,并且不知道从哪里开始,请随时通过我的电子邮件:[email protected]与我联系,我将为您提供启迪。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章