我想在这里问大家,一旦用户存储在数据库中,我将如何处理向配置文件表添加一行。现在唯一发生的事情是在用户表中创建了一个新行,这是预期的,但我也想获取该用户的id并添加一个新行,并将其作为user_profiles表中的user_id字段。用户配置文件创建
/**
* Save a new user.
*
* @param UserRequest $request
* TODO: Find a way to add a profile to a user once the user is saved.
*
* @return Response
*/
public function store(UserRequest $request)
{
$user = $this->userRepository->create($request->all());
return redirect('users');
}
创建方法位于基本存储库内,该存储库称为EloquentRepository,userRepository从中延伸而来。
/**
* Create a new modal instance in the database.
*
* @param array $data Attributes to be saved for the user.
*
* @return mixed
*/
public function create(array $data)
{
return $this->model->create($data);
}
编辑:
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password', 60);
$table->integer('user_role_id')->unsigned();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_role_id')->references('id')->on('user_roles');
});
Schema::create('user_profiles', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->text('bio')->nullable();
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->integer('postcode')->nullable();
$table->string('country')->nullable();
$table->string('phone')->nullable();
$table->timestamp('birthday')->nullable();
$table->string('facebook_username')->unique()->nullable();
$table->string('twitter_username')->unique()->nullable();
$table->string('google_plus_username')->unique()->nullable();
$table->string('behance_username')->unique()->nullable();
$table->string('pinterest_username')->unique()->nullable();
$table->string('linkedin_username')->unique()->nullable();
$table->string('github_username')->unique()->nullable();
$table->string('youtube_username')->unique()->nullable();
$table->string('instagram_username')->unique()->nullable();
$table->string('external_link')->unique()->nullable();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')->references('id')->on('users');
});
'$ user'应该有'$ user-> id',假设'id'是自动递增的字段名称。 –