我在Official Laravel 5.1 User Authentication创建了用户登录和用户注册,并且运行良好。
但是现在,我添加了另一个表 user_detail 表,其中包含用户的其他数据,如姓/名,性别等。此表格一对一与用户表的关系。我已经定义了 hasOne 和 belongsTo 等关系。
我想问一下,当我注册新用户时,用户和 user_detail 表格是如何填写的?虽然user_detail表只填充了'id',因为用户表和 user_detail 表具有相同的主键和外键ID。 / p>
此处参考路线:
...
// Registration routes...
Route::get('auth/register', 'Auth\AuthController@getRegister');
Route::post('auth/register', 'Auth\AuthController@postRegister');
...
AuthController :
<?php
namespace App\Http\Controllers\Auth;
use App\User;
use App\UsersDetail;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
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
*/
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',
]);
}
/**
* 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']),
]);
}
protected $redirectPath = '/dashboard';
}
答案 0 :(得分:1)
这是一种方法。
$user = new \User($data)->save();
$user_detail = new \UserDetail($detail_data);
$user->user_detail()->associate($user_detail);
答案 1 :(得分:0)
好的,我认为 @ajameswolf 方法是更新数据。我需要的是在两个有关系的表中创建数据。
我在 AuthController 中使用此方法。
if ($parent_id > 0) {
$sql = "SELECT * FROM recursive WHERE article_id = :article_id AND comment_parent = :parent_id ORDER BY comment_timestamp DESC";
} else {
$sql = "SELECT * FROM recursive WHERE article_id = :article_id AND comment_parent IS NULL ORDER BY comment_timestamp DESC";
}