php - 在 Laravel 5.5 中找不到 User::create()
问题描述
当我尝试注册用户时,出现错误:
Call to undefined method App\Models\User::create()
这是我的用户模型:
<?php
namespace App\Models;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'first_name', 'last_name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
protected $table = 'users';
}
我使用了 Laravel 5.5 附带的 User 模型,但只是将它移到了 Models 文件夹中。我更新了 config/auth 文件以指向 App\Models\User。我已经多次运行 php artisan optimize 和 composer dump-autoload ,但无济于事。
这是我的注册控制器:
<?php
namespace App\Http\Controllers\Auth;
use App\Models\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
class RegisterController extends Controller {
public $titles = [];
public $title = 'Registration';
use RegistersUsers;
/**
* Where to redirect users after registration.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest');
}
/**
* Get a validator for an incoming registration request.
*
* @param array $data
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function validator(array $data)
{
return Validator::make($data, [
'first_name' => 'required|string|max:255',
'last_name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6|confirmed',
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return \App\User
*/
protected function create(array $data)
{
return User::create([
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
public function showRegistrationForm() {
return view('auth.register')
->with('env', $this->env)
->with('titles', $this->titles)
->with('title', $this->title);
}
}
解决方案
从 Auth 包中扩展 Laravel User 模型时,您应该从该包中扩展 User 类。包中的抽象类扩展了模型,然后您将可以访问所有模型方法。
从 Illuminate\Foundation\Auth\User:
class User extends Model implements
AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
}
推荐阅读
- java - 与 JOptionPane 陷入循环
- python - 如何在 Django 的同一页面上使用登录表单和注册表单?
- javascript - 在 Javascript 中为对象数组创建不同的原点
- python - python - tkinter 命令 = 嵌套 for 循环中的 lambda 后期绑定问题
- python - 如何在 python 目录中添加所有包含 .h 文件
- javascript - 如何在不复制数组的情况下创建对现有数组的新引用
- php - 如何创建自定义用户来管理 WordPress 中的用户组?
- apache - Apache/Flask:增加并发请求
- hadoop - 鉴于 FALCON 已弃用,Hadoop 集群之间的增量复制(使用 Spark)
- php - 如何用另一个变量替换所有全局变量?