簡體   English   中英

Laravel - 將users表中的主鍵值存儲在users表的外鍵中

[英]Laravel - store the value of primary key from persons table in the foreign key of users table

我正在使用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匹配。

使用查詢構建器,您可以獲取插入的最后一條記錄的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()

希望有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM