欢迎!我有一个问题,我尝试使用此 https://github.com/cmgmyr/laravel-messenger Laravel软件包在Laravel 5.2上进行私人消息传递。当我没有任何收件人时,我可以发布消息,但是当我将新用户添加到数据库并且收件人现在以表单显示时,我有一个错误:试图获取非对象的属性。所有控制器和视图均从上述链接的示例复制。
问候
消息控制器:
<?php
namespace AppHttpControllers;
use AppUser;
use CarbonCarbon;
use CmgmyrMessengerModelsMessage;
use CmgmyrMessengerModelsParticipant;
use CmgmyrMessengerModelsThread;
use IlluminateDatabaseEloquentModelNotFoundException;
use IlluminateSupportFacadesAuth;
use IlluminateSupportFacadesInput;
use IlluminateSupportFacadesSession;
class MessagesController extends Controller
{
/**
* Show all of the message threads to the user.
*
* @return mixed
*/
public function index()
{
$currentUserId = Auth::user()->id;
// All threads, ignore deleted/archived participants
$threads = Thread::getAllLatest()->get();
// All threads that user is participating in
// $threads = Thread::forUser($currentUserId)->latest('updated_at')->get();
// All threads that user is participating in, with new messages
// $threads = Thread::forUserWithNewMessages($currentUserId)->latest('updated_at')->get();
return view('messenger.index', compact('threads', 'currentUserId'));
}
/**
* Shows a message thread.
*
* @param $id
* @return mixed
*/
public function show($id)
{
try {
$thread = Thread::findOrFail($id);
} catch (ModelNotFoundException $e) {
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
return redirect('messages');
}
// show current user in list if not a current participant
// $users = User::whereNotIn('id', $thread->participantsUserIds())->get();
// don't show the current user in list
$userId = Auth::user()->id;
$users = User::whereNotIn('id', $thread->participantsUserIds($userId))->get();
$thread->markAsRead($userId);
return view('messenger.show', compact('thread', 'users'));
}
/**
* Creates a new message thread.
*
* @return mixed
*/
public function create()
{
$users = User::where('id', '!=', Auth::id())->get();
return view('messenger.create', compact('users'));
}
/**
* Stores a new message thread.
*
* @return mixed
*/
public function store()
{
$input = Input::all();
$thread = Thread::create(
[
'subject' => $input['subject'],
]
);
// Message
Message::create(
[
'thread_id' => $thread->id,
'user_id' => Auth::user()->id,
'body' => $input['message'],
]
);
// Sender
Participant::create(
[
'thread_id' => $thread->id,
'user_id' => Auth::user()->id,
'last_read' => new Carbon,
]
);
// Recipients
if (Input::has('recipients')) {
$thread->addParticipant($input['recipients']);
}
return redirect('messages');
}
/**
* Adds a new message to a current thread.
*
* @param $id
* @return mixed
*/
public function update($id)
{
try {
$thread = Thread::findOrFail($id);
} catch (ModelNotFoundException $e) {
Session::flash('error_message', 'The thread with ID: ' . $id . ' was not found.');
return redirect('messages');
}
$thread->activateAllParticipants();
// Message
Message::create(
[
'thread_id' => $thread->id,
'user_id' => Auth::id(),
'body' => Input::get('message'),
]
);
// Add replier as a participant
$participant = Participant::firstOrCreate(
[
'thread_id' => $thread->id,
'user_id' => Auth::user()->id,
]
);
$participant->last_read = new Carbon;
$participant->save();
// Recipients
if (Input::has('recipients')) {
$thread->addParticipant(Input::get('recipients'));
}
return redirect('messages/' . $id);
}
}
标准路线:
Route::group(['prefix' => 'messages'], function () {
Route::get('/', ['as' => 'messages', 'uses' => 'MessagesController@index']);
Route::get('/create', ['as' => 'messenger.create', 'uses' => 'MessagesController@create']);
Route::post('/', ['as' => 'messages.store', 'uses' => 'MessagesController@store']);
Route::get('{id}', ['as' => 'messages.show', 'uses' => 'MessagesController@show']);
Route::put('{id}', ['as' => 'messages.update', 'uses' => 'MessagesController@update']);
});
和标准视图:
{!! Form::open(['route' => 'messages.store']) !!}
<div class="col-md-6">
<!-- Subject Form Input -->
<div class="form-group">
{!! Form::label('subject', 'Subject', ['class' => 'control-label']) !!}
{!! Form::text('subject', null, ['class' => 'form-control']) !!}
</div>
<!-- Message Form Input -->
<div class="form-group">
{!! Form::label('message', 'Message', ['class' => 'control-label']) !!}
{!! Form::textarea('message', null, ['class' => 'form-control']) !!}
</div>
@if($users->count() > 0)
<div class="checkbox">
@foreach($users as $user)
<label title="{{ $user->name }}"><input type="checkbox" name="recipients[]" value="{{ $user->id }}">{!!$user->name!!}</label>
@endforeach
</div>
@endif
<!-- Submit Form Input -->
<div class="form-group">
{!! Form::submit('Submit', ['class' => 'btn btn-primary form-control']) !!}
</div>
</div>
{!! Form::close() !!}
</div>