Garg Garg - 3 months ago 19
PHP Question

Messages view show same username on from/to fields in Laravel

I have messaging system where users can write messages to administrator. The system works great except when Administrator open to read some message the name which should display from who is the message(username) shows the admin username instead.

On the front end user side all is correct.

I can't see where is the problem. Here is the admin message controller

public function messagesView($userId) {
/** @var User $user */
$user = User::where('user_id', $userId)->first();
if (!$user) {
App::abort(404);
}
$messages = $user->messages()->orderBy('created_at', 'asc')->get();
return View::make('site.admin.messages_view', [
'messages' => $messages
]);
}

public function messagesViewSubmit($userId) {
/** @var User $user */
$user = User::where('user_id', $userId)->first();
if (!$user) {
App::abort(404);
}

$validatorRules = array(
'message' => 'required|min:5',
);

Input::merge(array_map('trim', Input::all()));
$validator = Validator::make(Input::all(), $validatorRules);

if ($validator->fails()) {
return Redirect::to('/admin/messages/view/' . $userId)->withErrors($validator->errors())->withInput(Input::all());
}

$message = new Message;
$message->user_id = $userId;
$message->text = Input::get('message');
$message->read_state = 0;
$message->from_admin = 1;
$message->save();
return Redirect::to('/admin/messages/view/' . $userId)->with('message_success', 'Message sent.');
}


Here is the view

@foreach($messages as $message)
@if($message->from_admin)
<div class="row">
<div class="media well col-xs-9">
<div class="media-left">
<img class="media-object" src="{{ URL::to('/img/admin.png') }}" alt="">
</div>
<div class="media-body user-message">
<h4 class="media-heading"><span style="font-weight: bold; color: red">Administrator {{{ $user['user_id'] }}}</span></h4>
<span>{{ $message->created_at }}</span>
<hr class="hr-sm-gray" />
{{ nl2br($message->text) }}
</div>
</div>
</div>
@else
<div class="row">
<div class="media well col-xs-9 col-xs-offset-3">
<div class="media-body text-right user-message">
<h4 class="media-heading">{{{ $user['username'] }}} - {{{ $user['user_id'] }}}</h4>
<span>{{ $message->created_at }}</span>
<hr class="hr-sm-gray" />
{{ nl2br(e($message->text)) }}
</div>
<div class="media-right">
<img class="media-object" src="{{ URL::to('/img/user.png') }}" alt="">
</div>
</div>
</div>
@if($message->read_state == 0)
{{ $message->markAsRead() }}
@endif
@endif
@endforeach


Messages model

protected $table = 'messages';
protected $primaryKey = 'message_id';

public function user() {
return $this->belongsTo('User', 'user_id', 'user_id');
}

public function markAsRead() {
$this->read_state = '1';
$this->save();


And this I have in User model

public function messages() {
return $this->hasMany('Message', 'user_id', 'user_id');
}


May be is session related.. here is what I have in BaseController where I check if user is logged in and if is admin or not.

protected function setupLayout()
{
if (!is_null($this->layout))
{

$this->layout = View::make($this->layout);
}

$user = self::getCurrentUser();
View::share('isLoggedIn', self::isLoggedIn());
View::share('user', $user);
if (self::isLoggedIn()) {
View::share('messagesCount', $user->messages()->where('read_state', '0')->where('from_admin', '1')->count());
}
}

public static function isLoggedIn() {
$user = Session::get('user', null);
if ($user !== null) {
return true;
} else {
return false;
}
}

public static function isAdmin() {
if (!self::isLoggedIn()) {
return false;
}

$user = self::getCurrentUser();
return (bool)$user->is_admin;
}

public static function getCurrentUser() {
if (!self::isLoggedIn()) {
return null;
}

$user = Session::get('user', null);
if (self::$user == null) {
$user = User::where('user_id', $user['user_id'])->first();
self::$user = $user;
}

return self::$user;
}

Answer

It is because you select current user username in else block

 <h4 class="media-heading">{{{ $user['username'] }}} - {{{ $user['user_id'] }}}</h4>

Try to replace it to

<h4 class="media-heading">{{{ $message->user->username }}} - {{{ $user['user_id'] }}}</h4>

This way you will select user from users table.

Comments