cannot use Auth::user() in laravel 4.2

451 views Asked by At

I'm working with laravel 4.2 to make a login page and home page. My login page works, then I want to display the username that of the logged in user. here is my code:

LoginController :

public function doLogIn(){
        $rules = array(
            'username'    => 'required|min:3',
            'password' => 'required|min:3'
        );

        $validator = Validator::make(Input::all(), $rules);
        if ($validator->fails()) {
            return 'Wrong Require';
        }
        else{

            $user = array(
                'username' => Input::get('username'),
                'password' => Input::get('password')
            );

            // $password = Hash::make('password');

            if (Auth::attempt($user)) 
            {
                return Redirect::intended('home');
                //return View::make('home.home');
            }
            else
            {
                return 'Wrong Password';
            }
        }
    }

and this is my home page that I want to display the username

Home.blade.php:

@section('header')
    <ul class="nav navbar-nav navbar-right">
       <li class="dropdown">
          <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">{{ Auth::user() }}<span class="caret"></span></a>
          <ul class="dropdown-menu" role="menu">
            <li><a href="#">Sigh Out</a></li>
          </ul>
        </li>
      </ul>
@endsection

Error happen on {{ Auth::user() }}

this is error I get

enter image description here

I'm sure I override $primaryKey in User Model (I mannually create table in mysql not use laravel )

this is my model but it's not correct because I write some part I remember

<?php 
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    protected $table = 'user';
    protected $primaryKey = 'userid';
    protected $fillable = array('username','password');
    protected $hidden = array('username','password');

    //..... and some method require for each implement class
}

Thank you.

2

There are 2 answers

3
Condoras On

You should use:

{{ Auth::user()->name }}
2
esh On

why don't use id instead of userid? Can you desc your users table?