jeudi 25 février 2016

Getting a 500 Internal Server Error with Laravel Chat Web Application

So I was watching this tutorial on creating a chat app with Laravel and Ajax, and I followed everything letter to letter, except for the class Chat extends \Eloquent which I added since it works in my version of Laravel 5.1.

I will try to add everything that is in my application. I have read probably ~15 other questions and all they say is adding a verification token, and I tried, but still to no avail.

This is my console upon load of the app. enter image description here

routes.php

Route::get('/{username}', function ($username) {
    return View::make('chats')->with('username', $username);
});
//ok
Route::post('sendMessage', array('uses' => 'ChatController@sendMessage'));
Route::post('isTyping', array('uses' => 'ChatController@isTyping'));
Route::post('notTyping', array('uses' => 'ChatController@notTyping'));
Route::post('retrieveChatMessages', array('uses' => 'ChatController@retrieveChatMessages'));
Route::post('retrieveTypingStatus', array('uses' => 'ChatController@retrieveTypingStatus'));

chats.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Chats</title>
    <link href="{{asset('css/bootstrap.min.css')}}" rel="stylesheet">
    <link href="{{asset('css/chats.css')}}" rel="stylesheet">
    <meta name="csrf-token" content="{{ csrf_token() }}">
</head>
<body>

    <div class="col-lg-4 col-lg-offset-4">
        <h1 id="greeting">Hello, <span id="username">{{$username}}</span></h1>

        <div id="chat-window" class="col-lg-12">

        </div>
        <div class="col-lg-12">
            <div id="typingStatus" class="col-lg-12" style="padding: 15px"></div>
            <input type="text" id="text" class="form-control col-lg-12" autofocus="" onblur="notTyping();">
            <input id="button" type="button" value="send" onclick="sendMessage();" />
        </div>
    </div>

    <script src="{{URL::asset('js/jquery-1.11.1.min.js')}}"></script>
    <script src="{{URL::asset('js/chats.js')}}"></script>
</body>
</html>

My Chat.php Model, which I created in app/

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class Chat extends \Eloquent
{
  protected $table = 'chat';
}

?>

ChatMessage.php Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use DB;

class Chat extends \Eloquent
{
  protected $table = 'chat_messages';
}

?>

my ChatController.php located in app/http/controllers

<?php

namespace App\Http\Controllers;

use App\Chat;
use App\ChatMessage;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Illuminate\Support\Facades\Redirect;
use View;
use Illuminate\Support\Facades\Input;

class ChatController extends Controller
{
  public function sendMessage()
  {
    $username = Input::get('username');
    $text = Input::get('text');

    $chatMessage = new ChatMessage();
    $chatMessage->sender_username = $username;
    $chatMessage->message = $text;
    $chatMessage->save();

  }

  public function isTyping()
  {
    $username = Input::get('username');

    $chat = Chat::find(1);
    if($chat->user1 == $username) {
      $chat->user1_is_typing = true;
    } else {
      $chat->user2_is_typing = true;
    }

    $chat->save();

  }

  public function notTyping()
  {
    $username = Input::get('username');

    $chat = Chat::find(1);
    if($chat->user1 == $username) {
      $chat->user1_is_typing = false;
    } else {
      $chat->user2_is_typing = false;
    }

    $chat->save();

  }

  public function retrieveChatMessages()
  {
    $username = Input::get('username');

    $message = ChatMessage::where('sender_username', '!=', $username)->where('read', '=', false)->first();

    if(count($message) > 0)
    {
      $message->read = true;
      $message->save();

      return $message->message;
    }
  }

  public function retrieveTypingStatus()
  {
    $username = Input::get('username');

    $chat = Chat::find(1);

    if($chat->user1 == $username) {
      if($chat->user2_is_typing) {
        return $chat->user2;
      }
    } else {
      if($chat->user1_is_typing) {
        return $chat->user1;
      }
    }

   }


}

?>

my chats.js

var username;



$(document).ready(function()
{
    username = $('#username').html();

    pullData();



    $(document).keyup(function(e) {
        if (e.keyCode == 13) {
          sendMessage();

        }

        else
            {isTyping();}
    });
});

function pullData()
{
    retrieveChatMessages();
    retrieveTypingStatus();
    setTimeout(pullData,3000);
}

function retrieveChatMessages()
{
    $.post('http://localhost/chat/public/index.php/retrieveChatMessages', {username: username}, function(data)
    {
        if (data.length > 0)
            {$('#chat-window').append('<br><div>'+data+'</div><br>');}
    });
}

function retrieveTypingStatus()
{
    $.post('http://localhost/chat/public/index.php/retrieveTypingStatus', {username: username}, function(username)
    {
        if (username.length > 0)
            {$('#typingStatus').html(username+' is typing');}
        else
            {$('#typingStatus').html('');}
    });
}

function sendMessage()
{

    var text = $('#text').val();

    if (text.length > 0)
    {
        $.post('http://localhost/chat/public/index.php/sendMessage', {text: text, username: username}, function()
        {

            $('#chat-window').append('<br><div style="text-align: right">'+text+'</div><br>');
            $('#text').val('');
            notTyping();
        });
    }
}

function isTyping()
{
    $.post('http://localhost/chat/public/index.php/isTyping', {username: username});
}

function notTyping()
{
    $.post('http://localhost/chat/public/index.php/notTyping', {username: username});
}

Both my migrations: 2016_02_25_070212_create_chatmessages_table.php

public function up()
    {
        Schema::create('chat_messages', function(Blueprint $table) {
          $table->increments('id');
          $table->string('sender_username');
          $table->text('message');
          $table->boolean('read')->default(false);
          $table->timestamps();
        });
    }

and: 2016_02_25_065913_create_chats_table.php

  public function up()
    {
        Schema::create('chats', function(Blueprint $table) {

          $table->increments('id');
          $table->string('user1');
          $table->string('user2');
          $table->boolean('user1_is_typing')->default(false);
          $table->boolean('user2_is_typing')->default(false);
          $table->timestamps();

        });
    }

Also this is my Kernel.php file, if for some reason it is needed.

<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
    /**
     * The application's global HTTP middleware stack.
     *
     * These middleware are run during every request to your application.
     *
     * @var array
     */
    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class,

        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

    /**
     * The application's route middleware.
     *
     * These middleware may be assigned to groups or used individually.
     *
     * @var array
     */
    protected $routeMiddleware = [
        'auth' => \App\Http\Middleware\Authenticate::class,
        'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
        'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
        'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    ];
}

I have absolutely no idea what to do from hereon. I have scoured google hoping to find an answer that works, so I would greatly appreciate any help, thank you in advance.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire