Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERROR: Trying to get property of non-object #102

Open
aarontanzk opened this issue Dec 15, 2017 · 0 comments
Open

ERROR: Trying to get property of non-object #102

aarontanzk opened this issue Dec 15, 2017 · 0 comments

Comments

@aarontanzk
Copy link

aarontanzk commented Dec 15, 2017

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');
});

`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant