I am using laravel 5.4 and Auth::login($user) is showing Type error: Argument 1 passed to Illuminate\Auth\SessionGuard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Database\Eloquent\Builder given, called in /home/vendor/laravel/framework/src/Illuminate/Auth/AuthManager.php on line 294
My User.php file is:
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'role', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
}
My AuthController.php is:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Auth;
use Illuminate\Support\Facades\Input;
use \Hash;
class AuthController extends Controller
{
//
public function register(Request $request){
$this->validate($request, [
'name' => 'required|max:30',
'email' => 'required|email|unique:users',
'regpassword' => 'required|alpha_num|confirmed'
]);
/*$user = new User();
$user['name'] = $request['name'];
$user['email'] = $request['email'];
$user['password'] = bcrypt($request['password']);
$user['role'] = 'user';
$user->save();
*/
$user = User::create(array(
'email' => Input::get('email'),
'name' => Input::get('name'),
'password' => Hash::make(Input::get('password')),
'role' => 'user'));
return 'success';
}
public function userLogin(Request $request){
$this->validate($request,[
'email' => 'required|email',
'password' => 'required'
]);
$user = User::where('email', '=', $request['email'])-> where('password' ,'=', Hash::make($request['password']))->where('role','=','user');
if($user){
Auth::login($user);
return $next($request);
}
else{
return redirect()->back();
}
}
}
My web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController@index');
Route::post('/register', 'AuthController@register');
Route::post('/UserLogin','AuthController@userLogin');
via Lakshya Garg