Messages view show same username on from/to fields in Laravel
<p>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. </p>

<p>On the front end user side all is correct.</p>

<p>I can't see where is the problem. Here is the admin message controller</p>

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

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

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

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

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

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

<p>Here is the view </p>

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

<p>Messages model</p>

<pre><code>protected $table = 'messages';
protected $primaryKey = 'message_id';

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

public function markAsRead() {
    $this-&gt;read_state = '1';
    $this-&gt;save();
</code></pre>

<p>And this I have in User model</p>

<pre><code>public function messages() {
    return $this-&gt;hasMany('Message', 'user_id', 'user_id');
}
</code></pre>

<p>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.</p>

<pre><code>protected function setupLayout()
{
    if (!is_null($this-&gt;layout))
    {

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

    $user = self::getCurrentUser();
    View::share('isLoggedIn', self::isLoggedIn());
    View::share('user', $user);
    if (self::isLoggedIn()) {
        View::share('messagesCount', $user-&gt;messages()-&gt;where('read_state', '0')-&gt;where('from_admin', '1')-&gt;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-&gt;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'])-&gt;first();
        self::$user = $user;
    }

    return self::$user;
}
</code></pre>