[英]Laravel 5.1 Authentication - Create user data in another table
我的身份验证正常,一切正常。 我可以登录/注销等。我有两个表,一个表称为用户,这是身份验证的默认值,第二个表称为播放器,将用于播放器数据。 我制作了一个Player模型。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Player extends Model
{
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'players';
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['username', 'wood'];
}
并将AuthController类编辑为:
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
*/
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, [
'username' => 'required|min:4|max:32|unique:players',
'name' => 'required|max:255',
'email' => 'required|email|max:255|unique:users',
'password' => 'required|confirmed|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']),
]);
return Player::create([
'username' => $data['username'],
'wood' => 0,
]);
}
}
当我注册一个新用户时,将在用户表中创建用户数据,但我的玩家表没有任何反应。 它只是空的。
这是因为在将数据插入到User表之后,您从方法中退出。 这里的代码错误:
return User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
需要:
protected function create(array $data)
{
User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
]);
return Player::create([
'username' => $data['username'],
'wood' => 0,
]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.