title: laravel private chat application
body: <p>Hello i'm a new developer and in my laravel project i have implemented a real time group chat  that uses ajax but now i need a real time chat between users in ajax i have tried many things but none have worken   it would be very helpful if you could help me or if could send me a link to a good tutorial </p>
  
  <p>Here's the index of my group chat :</p>
  
  <pre><code>@extends('admin.app')
  @section('content')
      &lt;div class="container"&gt;
          &lt;div class="row" style ="padding-top:40px;"&gt;
              &lt;h3 class="text-center"&gt;Welcome {{Auth::user()-&gt;FullName}}&lt;/h3&gt;
              &lt;br/&gt;&lt;br/&gt;
              &lt;div class="col-md-2"&gt;
                      &lt;p&gt;Users online&lt;/p&gt;
                      @foreach($users as $user)
                          @if($user-&gt;isOnline())
                              &lt;li&gt;{{$user-&gt;FullName}}&lt;/li&gt;
                          @endif
                      @endforeach
  
  
              &lt;/div&gt;
  
              &lt;div class="col-md-8"&gt;
                  &lt;div class="panel panel-info"&gt;
                      &lt;div class="panel-heading"&gt;
                          Recent Chat
                      &lt;/div&gt;
                      &lt;div class="panel-body"&gt;
                          &lt;ul class="media-list" id="message"&gt;
  
                              @foreach($messages as $message )
                                  &lt;li class="media"&gt;
                                      &lt;div class="media-body"&gt;
                                          &lt;div class="media"&gt;
  
                                              &lt;div class="media-body" &gt;
                                                  {{$message-&gt;message}}
                                                  &lt;br/&gt;
                                                 &lt;bold&gt; &lt;small class="text-muted"&gt;{{$message-&gt;from_name}} |{{$message-&gt;created_at}}
                                                     &lt;/small&gt;&lt;/bold&gt;
                                                  &lt;hr&gt;
                                              &lt;/div&gt;
                                          &lt;/div&gt;
                                      &lt;/div&gt;
                                  &lt;/li&gt;
  
  
                              @endforeach
                          &lt;/ul&gt;
                          &lt;div&gt;
                              &lt;div class="panel-footer"&gt;
                                  &lt;div class="input-group"&gt;
                                      &lt;input type="text" name="message" class="form-control" placeholder="Enter Message"/&gt;
                                      {{csrf_field()}}
                                      &lt;input type="hidden" name="from_name" value="{{Auth::user()-&gt;FullName}}"&gt;
                                      &lt;span class="input-group-btn"&gt;
                                      &lt;button type="submit" id="send" class="btn btn-info"&gt;Send&lt;/button&gt;
                                  &lt;/span&gt;
                                  &lt;/div&gt;
                              &lt;/div&gt;
                          &lt;/div&gt;
                          &lt;div class="col-md-2"&gt;
  
                          &lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  
      &lt;script type="text/javascript" src="/assets/admin/plugins/jquery/jquery-1.9.1.min.js"&gt;&lt;/script&gt;
      &lt;script type="text/javascript"&gt;
          $(document).ready(function(){
              setTimeout(realTime, 2000);
          });
          function realTime() {
              $.ajax({
                  type:'post',
                  url:'/chat/get',
                  data:{
                      '_token':$('input[name=_token]').val(),
                  },
                  success: function (data) {
                      $('#message').replaceWith(' &lt;ul class="media-list" id="message"&gt;&lt;/ul&gt;');
                      for (var i=0; i &lt; data.length; i++){
                          $('#message').append(' &lt;li class="media"&gt;&lt;div class="media-body"&gt;&lt;div class="media"&gt;&lt;div class="media-body"&gt;'+data[i].message+'&lt;br/&gt;&lt;small class="text-muted"&gt;'+data[i].from_name+'|'+ data[i].created_at+'&lt;/small&gt;&lt;hr/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;')
                      }
                  },
              });
              setTimeout(realTime, 2000);
          }
          $(document).on('click','#send', function (){
              $.ajax({
                  type:'post',
                  url:'/chat/send',
                  data:{
                      '_token':$('input[name=_token]').val(),
                      'from_name':$('input[name=from_name]').val(),
                      'message':$('input[name=message]').val(),
                  },
                  success: function (data) {
                    $('#message').append('  &lt;li class="media"&gt;&lt;div class="media-body"&gt;&lt;div class="media"&gt;&lt;div class="media-body"&gt;'+data.message+'&lt;br/&gt;&lt;small class="text-muted"&gt;'+data.from_name+'|'+ data.created_at+'&lt;/small&gt;&lt;hr/&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/li&gt;');
                  }
              })
  
              $('input[name=message]').val('');
          });
  
      &lt;/script&gt;
  
  @stop   
  </code></pre>
  
  <p>here are the routes of my group chat:</p>
  
  <pre><code>Route::get('/chat', 'Chat\ChatController@index')-&gt;name('chat.index');
  Route::post('/chat/send', 'Chat\ChatController@sendMessage' )-&gt;name('admin.chat.sendMessage');
  Route::post('/chat/get', 'Chat\ChatController@getMessage' );
  </code></pre>
  
  <p>here's my controllers of the group chat:</p>
  
  <pre><code>public function __construct()
  {
      $this-&gt;middleware('auth');
  }
  
  public function index()
  {
      $users=user::all();
      $messages=message::all();
      return view('admin.chat.index',['messages'=&gt; $messages],compact('users'));
  }
  
  public function sendMessage(Request $request){
  
      $send = new Message();
      $send -&gt;from_name = $request-&gt;from_name;
      $send -&gt;message = $request-&gt;message;
      $send-&gt;save();
      return response()-&gt;json($send);
  
  }
  
  public function  getMessage(){
      $message = Message::all();
      return response()-&gt;json($message);
  }
  </code></pre>
  
  <p>here the migration for my gruop chat :</p>
  
  <pre><code>&lt;?php
  
  use Illuminate\Support\Facades\Schema;
  use Illuminate\Database\Schema\Blueprint;
  use Illuminate\Database\Migrations\Migration;
  
  class CreateMessagesTable extends Migration
  {
      /**
       * Run the migrations.
       *
       * @return void
       */
      public function up()
      {
          Schema::create('messages', function (Blueprint $table) {
              $table-&gt;increments('id');
              $table-&gt;string('from_name');
              $table-&gt;text('message');
              $table-&gt;timestamps();
          });
      }
  
      /**
       * Reverse the migrations.
       *
       * @return void
       */
      public function down()
      {
          Schema::dropIfExists('messages');
      }
  }
  </code></pre>
  
  <p>Sorry in advance for the bad english and it would very grateful if you could help me or if you could send the link of a good tutorial </p>