Laravel 5.2 错误 App\User 不能使用 Illuminate\Foundation\Auth\User - 这不是特征
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37236394/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.2 Error App\User cannot use Illuminate\Foundation\Auth\User - it is not a trait
提问by Arun
I am pretty new to laravel 5.2. Right now I am trying to create a custom login. I created the login html. And on submit, the function shows error
我对 Laravel 5.2 很陌生。现在我正在尝试创建自定义登录。我创建了登录html。并在提交时,该函数显示错误
App\User cannot use Illuminate\Foundation\Auth\User - it is not a trait
App\User 不能使用 Illuminate\Foundation\Auth\User - 这不是特征
I couldn't find what is the error. The codes are below
我找不到错误是什么。代码如下
Routes.php
路由.php
Route::post('/', 'Admin\LoginController@postLogin');
LoginController.php
登录控制器.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use Auth;
use Request;
class LoginController extends Controller {
public function index() {
return view('admin.login');
}
public function register() {
return view('admin.register');
}
public function postLogin() {
$email = Request::input('email');
$password = Request::input('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) {
//echo "success";
return redirect('admin/dashboard');
} else {
return redirect('/');
}
}
}
User.php
用户名.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable,
CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['keyword', 'remember_token'];
}
Auth.php
验证文件
<?php
return [
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'users' => [
'driver' => 'eloquent',
'model' => 'App\User',
'table' => 'users',
'password' => [
'email' => 'emails.password',
'expire' => 60,
],
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
];
I am following the tutorial from here
我正在关注这里的教程
Please help me. I am stuck here from last few hours
请帮我。我从过去几个小时被困在这里
回答by Alexey Mezenin
You should use this namespace:
你应该使用这个命名空间:
use Illuminate\Auth\Authenticatable;
It's a trait you should use. Now, you're trying to use a class as a trait:
这是您应该使用的特征。现在,您正在尝试使用类作为特征:
use Illuminate\Foundation\Auth\User as Authenticatable;
class User {
use Authenticatable ...