我正在尝试使用注册开箱即用注册Laravel 5.1中的用户。 我完成了tutorial
中的所有内容当我填写我的注册表并提交时,我得到:
AuthController.php第62行中的FatalErrorException:调用未定义的方法App \ User :: create()
该方法是Laravel 5.1中的内置方法:
protected function create(array $data)
{
return User::create([ // <!-- it fails here
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
有什么想法吗?
以下是我的routes.php
,以防:
// Authentication routes...
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
//
//
// Password reset link request routes...
Route::get('password/email', 'Auth\PasswordController@getEmail');
Route::post('password/email', 'Auth\PasswordController@postEmail');
// Password reset routes...
Route::get('password/reset/{token}', 'Auth\PasswordController@getReset');
Route::post('password/reset', 'Auth\PasswordController@postReset');
Route::group(['middleware' => 'auth'], function () {
Route::get('/', 'DashboardController@index');
Route::resource('tournaments', 'TournamentController');
Route::resource('competitors', 'CompetitorController');
Route::resource('grade', 'GradeController');
Route::resource('users', 'UserController');
Route::resource('country', 'CountryController');
Route::resource('places', 'PlaceController');
});
我的AuthController
:
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
class AuthController extends Controller
{
/*
|--------------------------------------------------------------------------
| Registration & Login Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users, as well as the
| authentication of existing users. By default, this controller uses
| a simple trait to add these behaviors. Why don't you explore it?
|
*/
use AuthenticatesAndRegistersUsers, ThrottlesLogins;
/**
* Create a new authentication controller instance.
*
* @return void
*/
private $redirectTo = '/';
public function __construct()
{
$this->middleware('guest', ['except' => 'getLogout']);
}
/**
* 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, [
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|min:6',
'password_confirmation' => 'required|min:6'
]);
}
/**
* Create a new user instance after a valid registration.
*
* @param array $data
* @return User
*/
protected function create(array $data)
{
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
}
}
答案 0 :(得分:0)
问题1:
您应该在路线中定义create
方法
Route::post('create', 'yourController@create');
问题2:
我猜您正在混合get和post,如果您正在使用get方法,那么您需要通过url传递所有数据,这似乎不太好,我建议您使用post
方法相反,所以我用post方法写了路线
public function create()
{
$userData = Input::except('_token');
User::create($userData);
}
警告:您未验证输入
您可以通过创建
来完成此操作$validation = Validator::make($userData, User::$userData);
if ($validation->passes())
{
//do Create Action here and return success
}
{
//return failure messages to view
}
您可以在$postData
模型中定义User
规则。
更新:我发现您使用默认AuthenticatesAndRegistersUsers
感谢Thomas Kim指向它。
在create()方法中
您将获得输入并打印出来进行测试
$userData = Input::except('_token');
return $userData;
然后完成(插入)
User::create($userData);
希望这能帮到你
答案 1 :(得分:0)
我的用户模型没有扩展模型!问题解决了!