用户配置文件创建
我想在这里问大家,一旦用户存储在数据库中,我将如何处理向配置文件表添加一行。现在唯一发生的事情是在用户表中创建了一个新行,这是预期的,但我也想获取该用户的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');
});
/**
* 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());
//if you are doing what I think this should return the current created user - return $this->model->create($data); which is passed to $user
$user_id = $user->id;
//do something with the user_id, example, assuming you have this profileRepository
$this->profileRepository->create([
'user_id'=>$user_id
]);
return redirect('users');
}
这几乎是我想要的,但$ user在userrepository的create语句之后持续返回null($ user)。 – user3732216
用户表中的id是否自动递增? – Digitlimit
我回头对不起,我忘了我最初覆盖用户repo中的创建函数,所以我重新运行它,我收到用户对象很好,但我得到了SQLSTATE [23000]:完整性约束违规:1452无法添加或更新子行:外键约束失败('backstage'.'user_profiles',CONSTRAINT'user_profiles_user_id_foreign' FOREIGN KEY('user_id')REFERENCES'users'('id'))(SQL:insert into'user_profiles'( 'updated_at','created_at')values(2015-04-17 14:13:02,2015-04-17 14:13:02)) – user3732216
有一个PHP函数mysql_insert_id获得最后插入的ID。通过使用该ID您可以在user_profiles
表中使用。
例子:
$query = "INSERT INTO test (value) VALUES ('test')";
mysql_query($query);
$lastId = mysql_insert_id();// it will return the executed result of the query.
$query1 = "INSERT INTO test1 (user_id) VALUES ('$lastId')";
这是不准确,你想要什么,但是这会帮助你通过一些其他的方式。
我知道这件事,但不知道我在找什么抱歉。 – user3732216
'$ user'应该有'$ user-> id',假设'id'是自动递增的字段名称。 –