如何使用Laravel显示从其他用户收到的消息

时间:2018-04-26 09:18:40

标签: laravel laravel-5 chat laravel-5.5

我正在我的网站上处理两个用户之间的消息传递功能,并且我已设法使消息传递系统正常工作。但我有一个小问题,我无法找到解决方案。我希望能够显示,当用户点击此路线/对话时,从其他用户收到的所有消息的列表。但是现在它的作用是当我点击我不想要的路线/对话时,它会显示用户表中所有用户的列表。

以下是我在网上的路线

Route::get('/conversations', 'ConversationsController@index')->name('conversations');

Route::get('/conversations/{user}', 'ConversationsController@show')->name('conversations.show');

Route::post('/conversations/{user}', 'ConversationsController@store');

以下是我的索引

的会话路由列表
<div class="contact-edit" id="ci">
 <h3>MEssages</h3>

   @include('conversations.users', ['users'=>$users, 'unread'=>$unread])

</div>

这是conversation.users

<div class="col-md-3">

        <div class="list-group">


            @foreach($users as $user)
            <a class = "list-group-item d-flex justify-content-between align-items-center" href="{{route('conversations.show', $user->id)}}">
                {{ $user->name }} 

                @if(isset($unread[$user->id]))
                    <span class="badge-pill badge-primary">{{$unread[$user->id]}}</span>
                @endif

            </a>

            @endforeach


        </div>

以下是打开对话标签的路线

<a href="{{ route('conversations.show', ['id'=>$profiledetail->id]) }}">Ecrire un message</a>

这是我的对话控制器

类ConversationsController扩展Controller {

private $r;

private $auth;


public function __construct(ConversationRepository $conversationRepository, AuthManager $auth)
{
    $this->r = $conversationRepository;
    $this->middleware('auth');
    $this->auth = $auth;
}



public function index (User $user){

    $me = Auth::user(); 

    //dd(Auth::user());
    return view('conversations/index', [
        'users'=>$this->r->getConversations(Auth::user()->id),
        'unread' => $this->r->unreadCount(Auth::user()->id)

    ]);
}


public function show (User $user){

 $me = Auth::user();   
 $messages = $this->r->getMessagesFor($me->id, $user->id)->paginate(5);
 $unread = $this->r->unreadCount($me->id);

 if (isset($unread[$user->id])) {
     $this->r->readAllFrom($user->id, $me->id);
     unset($unread[$user->id]);
 }

    return view('conversations/show', [
        'users'=>$this->r->getConversations($me->id),
        'user'=>$user,
        'messages'=>$messages,
        'unread' => $unread 
    ]);


}


public function store (User $user, StoreMessageRequest $request){

    $message = $this->r->createMessage(

        $request->get('content'), 
        Auth::user()->id,
        $user->id
    );

    $user->notify(new MessageReceived($message));

    return redirect(route('conversations.show', ['id'=>$user->id]));

}

}

这是我的对话库

class ConversationRepository 
   {

private $user;
private $message;

public function __construct(User $user, Message $message)
{
    $this->user = $user;
    $this->message = $message;
}

public function getConversations(int $userId)
{
    $conversations =  $this->user->newQuery()
        ->select('name','surname','photo','id')
        ->where('id', '!=', $userId)
        ->whereType('jobber')
        ->get();

    return $conversations;

}

public function createMessage(string $content, int $from, int $to)
{
    return $this->message->newQuery()->create([

        'content'=>$content,
        'from_id'=>$from,
        'to_id'=>$to,
        'created_at'=>\Carbon\Carbon::now()
    ]);

}

public function getMessagesFor(int $from, int $to) : Builder
{
    return $this->message->newQuery()

        ->whereRaw("((from_id = $from AND to_id = $to )OR(from_id = $to AND to_id = $from))")

        ->orderBy('created_at', 'DESC')

        ->with([

            'from'=> function ($query){ return $query->select('name', 'id');}

        ]);


}


//Recupere le nombre de message non lu pour chaque conversation

public function unreadCount (int $userId)
{

    return $this->message->newQuery()
        ->where('to_id', $userId)
        ->groupBy('from_id')
        ->selectRaw('from_id, COUNT(id) as count')
        ->whereRaw('read_at IS NULL')
        ->get()
        ->pluck('count', 'from_id');
}

这是我的用户模型

 public function messages()
{
    return $this->hasMany('App\Message', 'from_id', 'to_id', 'content');
}

这是消息模型

  public function user() 
{
    return $this->belongsTo('App\User');
}

欢迎任何帮助

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解您想要做什么,但这里有一个可能的解决方案,假设您想要获取所有给您的消息。

在您的存储库中:

$this->message->where('to_id',Auth::user()->id)->get();