ERROR: Trying to get property of non-object
aarontanzk opened this issue · comments
Hi
I'm using talk-example and integrated parts of it into a fresh laravel 5.3 installation.
When I go to /message/1 for chatHistory between two users, I get the above ERROR. There are no conversations in the database.
MessageController.php
`<?php
namespace App\Http\Controllers;
use App\User;
use Illuminate\Http\Request;
use Nahid\Talk\Facades\Talk;
use Auth;
use View;
class MessageController extends Controller
{
protected $authUser;
public function __construct()
{
$this->middleware('auth');
Talk::setAuthUserId(Auth::user()->id);
View::composer('partials.peoplelist', function($view) {
$threads = Talk::threads();
$view->with(compact('threads'));
});
}
public function chatHistory($id)
{
$conversations = Talk::getMessagesByUserId($id, 0, 5);
$user = '';
$messages = [];
if(!$conversations) {
$user = User::find($id);
} else {
$user = $conversations->withUser;
$messages = $conversations->messages;
}
return view('messages.conversations', compact('messages', 'user'));
}
public function ajaxSendMessage(Request $request)
{
if ($request->ajax()) {
$rules = [
'message-data'=>'required',
'_id'=>'required'
];
$this->validate($request, $rules);
$body = $request->input('message-data');
$userId = $request->input('_id');
if ($message = Talk::sendMessageByUserId($userId, $body)) {
$html = view('ajax.newMessageHtml', compact('message'))->render();
return response()->json(['status'=>'success', 'html'=>$html], 200);
}
}
}
public function ajaxDeleteMessage(Request $request, $id)
{
if ($request->ajax()) {
if(Talk::deleteMessage($id)) {
return response()->json(['status'=>'success'], 200);
}
return response()->json(['status'=>'errors', 'msg'=>'something went wrong'], 401);
}
}
public function tests()
{
dd(Talk::channel());
}
}
`
web.php
`
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('tests', 'MessageController@tests');
Route::get('/home', 'HomeController@index');
Route::get('message/{id}', 'MessageController@chatHistory')->name('message.read');
Route::group(['prefix'=>'ajax', 'as'=>'ajax::'], function() {
Route::post('message/send', 'MessageController@ajaxSendMessage')->name('message.new');
Route::delete('message/delete/{id}', 'MessageController@ajaxDeleteMessage')->name('message.delete');
});
`