我正在使用Laravel框架。
我有2个表(用户和人员)。我想将users表的主键person_id存储在users表的外键person_id中。目前我使用 $ user-> person_id ='1';
表用户
CREATE TABLE IF NOT EXISTS `festival_aid`.`users` (
`user_id` BIGINT NOT NULL AUTO_INCREMENT,
`user_username` VARCHAR(45) NOT NULL,
`user_email` VARCHAR(45) NOT NULL,
`user_password` CHAR(32) NOT NULL,
`user_salt` CHAR(32) NOT NULL,
`user_created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`user_modified` TIMESTAMP NULL,
`user_deleted` TIMESTAMP NULL,
`user_lastlogin` TIMESTAMP NULL,
`user_locked` TIMESTAMP NULL,
`user_token` VARCHAR(128) NULL,
`user_confirmed` TIMESTAMP NULL,
PRIMARY KEY (`user_id`, `person_id`),
UNIQUE INDEX `user_email_UNIQUE` (`user_email` ASC),
INDEX `fk_users_persons1_idx` (`person_id` ASC),
CONSTRAINT `fk_users_persons1`
FOREIGN KEY (`person_id`)
REFERENCES `festival_aid`.`persons` (`person_id`)
ON DELETE CASCADE
ON UPDATE NO ACTION)
ENGINE = InnoDB;
表人
CREATE TABLE IF NOT EXISTS `festival_aid`.`persons` (
`person_id` BIGINT NOT NULL AUTO_INCREMENT,
`person_firstname` VARCHAR(45) NULL,
`person_surname` VARCHAR(45) NULL,
`person_created` TIMESTAMP NOT NULL,
`person_modified` TIMESTAMP NULL,
`person_deleted` TIMESTAMP NULL,
PRIMARY KEY (`person_id`))
ENGINE = InnoDB;
索引操作
public function index()
{
$person = Person::with('user')->orderBy('person_id')->paginate(10);
return View::make('persons.index')
->with('person', $person);
}
商店行动
public function store()
{
$input = Input::all();
$rules = array();
$validator = Validator::make($input, $rules);
if($validator->passes())
{
$password = $input['user_password'];
$password = Hash::make($password);
$person = new Person();
$user = new User();
$person->person_firstname = $input['person_firstname'];
$person->person_surname = $input['person_surname'];
$user->user_username = $input['user_username'];
$user->user_email = $input['user_email'];
$user->user_password = $password;
$person->save();
$user->person()->associate($person, 'person_id', 'user_id');
$user->save();
Session::flash('message', 'Successfully created user!');
return Redirect::to('persons/index');
}
else {
return Redirect::to('persons/create')->withInput()->withErrors($validator);
}
}
用户迁移
Schema::table('users', function(Blueprint $table)
{
$table->increments('user_id');
$table->string('user_email');
$table->timestamp('user_created');
$table->timestamp('user_modified');
$table->timestamp('user_deleted');
$table->timestamp('user_lastlogin');
$table->timestamp('user_locked');
$table->foreign('person_id')
->references('id')->on('persons')
->onDelete('cascade');
});
人员迁移
public function up()
{
Schema::table('persons', function(Blueprint $table)
{
$table->increments('person_id');
$table->string('person_firstname');
$table->string('person_surname');
});
}
模型用户
class User extends Eloquent {
protected $primaryKey = 'user_id';
public function persons()
{
return $this->hasOne('Person');
}
public $timestamps = false;
}
模特人
class Person extends Eloquent {
protected $table = 'persons';
protected $primaryKey = 'person_id';
public function users()
{
return $this->belongsTo('User');
}
public $timestamps = false;
}
我只想改变这个:$ user-> person_id ='1';因为我为每个用户创建了外键person_id将为1.我希望users表中的外键person_id与people表的主键person_id匹配。
答案 0 :(得分:2)
使用查询构建器,您可以获得插入的最后一条记录的ID:
$id = DB::table('users')->insertGetId(array('email' => 'john@example.com', 'votes' => 0));
但是如果您使用Eloquent,我认为您应该使用关联方法,这是从Laravel帮助中获取的代码示例,您应该将其调整为您的用户,人员逻辑:
$account = Account::find(10);
$user->account()->associate($account);
$user->save()
希望有所帮助。