mercredi 2 mai 2018

Laravel Inbox Messaging system

I am trying to implement a messaging system using Laravel. The sending and receiving of messages between two users is quite working fine But my only issue is to display all the messages received by a user on the /conversations view. When a user sends a message to another user, It shows a bar proving the message has been received but when you click on the tab it gets a different user id not the id of the sender. Don't know what is wrong.

Here is my Controller

 public function index (){
    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 
    ]);


}

I used a repository with this included

private $user;
private $message;

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

public function getConversations(int $userId)
{

    $conversations = $this->message->where('to_id', $userId)->get();

    //dd($conversations);

return $conversations;

}

Here is my View

<aside class="col-lg-3 column border-right">
    <div class="widget">
        <div class="tree_widget-sec">
        <div class="padding-left">
            <div class="contact-edit" id="ci">
                <h3>Messenger</h3>

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

                    <div class="list-group">


                        @foreach($users as $user)
                        <a class = "list-group-item d-flex justify-content-between align-items-center" href="">
                             

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

                        </a>

                        @endforeach


                    </div>

                </div>
            </div>
        </div>
    </div>
</div>
</aside>



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2w7vgGI
via IFTTT

Aucun commentaire:

Enregistrer un commentaire