Zubair
Zubair

Reputation: 295

I want to fetch chat messages from database using laravel

I am trying to fetch chat messages from database. Unfortunately, I am unable to fetch chat messages because I am facing an error. How can I fix it? Please help me, thanks.

    please see this error
    https://flareapp.io/share/v5pll05E#F52

Current status

            {"id":12,"name":"Faraz","email":"[email protected]",
           "email_verified_at":null,"job":null,"phone_number":null,
           "profile_image":null,"created_at":"2020-02-14 04:35:07","updated_at":"2020-02-14 
            04:35:07","chats":[{"id":1,"to_user_id":12,"from_user_id":13,"chat_message":"Hi 
            Well come to horizon technology","timestamp":"2020-02-12 17:54:55"}]}

User Modal

            class User extends Authenticatable
            {
            public function Chats()
            {
            return $this>hasMany('App\Chat_message','to_user_id','id');
            }

Chat_message model

            class Chat_message extends Model
            {  
            public function user()
            {
            return $this->belongsTo('App\User','id');
            }

Controller

             public function chat($id)
             {      
             $single_chat = User::with('Chats')->where('id',$id)->first();

             // return $single_chat;          
              return view('single_chat',compact('single_chat'));
              } 
              }

Html view

              @foreach($single_chat as $single_chats)
              <!-- Message to the right -->
              <div class="direct-chat-msg right">
              <div class="direct-chat-infos clearfix">
              <span class="direct-chat-name float-right">Sarah Bullock</span>
              <span class="direct-chat-timestamp float-left">23 Jan 6:10 pm</span>
              </div>
              <!-- /.direct-chat-infos -->
             <img class="direct-chat-img" src="{{url('public/assets/dist/img/user1- 
             128x128.jpg')}}" alt="message user image">
             <!-- /.direct-chat-img -->
             <div class="direct-chat-text">
             {{$single_chats->chat_message}}
             </div>
             <!-- /.direct-chat-text -->
             </div>
             <!-- /.direct-chat-msg -->
             </div>
             <!--/.direct-chat-messages-->
             @endforeach

Upvotes: 1

Views: 535

Answers (1)

julianstark999
julianstark999

Reputation: 3616

Problem 1:
In your controller you use ...->first() so you get only one model and not a collection, so you cant loop it with an @foreach loop inside your view.

@foreach($single_chat as $single_chats) should be @foreach($single_chat->chats as $chat), so you can loop through the chat_message of one user.

Problem 2:
Inside the loop you try to get your chat_message from $single_chats like this {{$single_chats->chat_message}}
If you use {{$chat->THE_MESSAGE_PROPERTY}} it should work, because we fixed the loop error in Problem 1

Upvotes: 1

Related Questions