我无法让用户在Laravel中登录。我使用username
代替email
。
以下是我的路线
use App\Message;
Route::get('/', function () {
return view('welcome');
});
//
Route::post('/contact','MessageController@store');
Route::get('/admin9419/dashboard','AdminDashboardController@index')->name('dash');
Route::get('/admin9419/login','SessionsController@create')->name('home');
Route::post('/admin9419/login','SessionsController@store');
Route::post('/admin9419/logout','SessionsController@destroy');
这是我的SessionController文件
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
class SessionsController extends Controller
{
public function __construct()
{
$this->middleware('guest');
}
public function create(){
return view('sessions.create');
}
public function destroy(){
auth()->logout();
return redirect()->home();
}
public function store(){
if(!auth()->attempt(['username'=>request('username'),'password'=>request('password')])){
return back();
}
return redirect('/admin9419/dashboard');
}
}
错误:
Type error: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable
用户类:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
//
}
任何人都可以帮助我错过的吗?
答案 0 :(得分:1)
代码似乎是正确的。
请参阅类用户,它应该是:
use App\Notifications\MailResetPasswordToken;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
....
}
那么你可以粘贴User类吗?
答案 1 :(得分:0)
默认情况下,Laravel使用电子邮件字段进行身份验证。如果您想自定义它,可以在LoginController上定义用户名方法:
public function username()
{
return 'username';
}
答案 2 :(得分:0)
public function store(Request $request){
$username= $request->input('username');
$password= $request->input('password');
if (Auth::guard('web')->attempt(['username' => $username , 'password'=> $password ])) {
return redirect()->intended('/admin9419/dashboard');
}
else{
return redirect('/login');
}
}