1

I want to do Auth::user() but before that I must do Auth::attempt() but it always returns false because the attempt() method uses ID to look for the user I am are trying to login. But in my users table I don't have a ID field rather I have user_id field. So is there a way to make the attempt() method use user_id to look up for id in my users table. (Hope I made some sense )

if(!Auth::attempt(Request::only(['username','password']))){  //returns false
     return redirect($this->redirectPath);   /// redirect to login page
}else{
     return redirect($this->redirectTo);   /// redirect to user list page
}
Slava Vedenin
  • 58,326
  • 13
  • 40
  • 59
Saurab
  • 1,931
  • 5
  • 20
  • 33
  • https://laracasts.com/discuss/channels/laravel/authuser-returns-null-in-laravel-52 – Alive to die - Anant Mar 04 '16 at 17:20
  • http://stackoverflow.com/questions/35190207/authuser-returns-null-in-laravel-5-2 – Alive to die - Anant Mar 04 '16 at 17:21
  • https://laracasts.com/discuss/channels/laravel/authuser-returns-null-in-laravel-52?page=0 – Alive to die - Anant Mar 04 '16 at 17:21
  • I am aware of the methods discussed there but in order to use Auth::user() I first need to login, but in my case i cannot even login because Auth::attempt() is used to authenticate login and it returns false because I don;t have id column in my table .. – Saurab Mar 04 '16 at 17:33
  • Did you try changing primary key in `User` model? `protected $primaryKey = 'user_id'` – user2094178 Mar 04 '16 at 20:32
  • yup .. didn't do any good .. – Saurab Mar 04 '16 at 21:03
  • Ok, but now chances are there is some problem with `attempt`. Replace `!Auth::attempt(Request::only(['username','password']))` with `!Auth::loginUsingId(1)` and see what happens. – user2094178 Mar 04 '16 at 23:19
  • That's not gonna do any good .. All laravel eloquent model uses Query string like "" SELECT * FROM WHERE id= . So, till i change my column name to id in my users table , I cannot use any of { attempt(), find(), loginUsingId()} .. I would have to hard code SQL if I don't want to change my column name
    – Saurab Mar 05 '16 at 04:25
  • @username you're doing something wrong. With `attempt` laravel tries to find the model by provided credentials (in this case just `username`, since password will be stripped down), no id. If you try with `loginUsingId` it will use the `primaryKey` of your model (which defaults to `id`, but can be changed). – Jarek Tkaczyk Mar 05 '16 at 15:03

0 Answers0