简体   繁体   English

创建具有ID的记录

[英]Create record with id

I need to import the data from a different database and transfer it to the new database. 我需要从其他数据库导入数据,并将其传输到新数据库。 but the ID information of the members is required for the relationship. 但是关系需要成员的ID信息。 I have to carry them too. 我也要带它们 Therefore, I have to fill in the ID column which is increased as AUTOINCREMENT. 因此,我必须填写ID列,该列增加为AUTOINCREMENT。

My Migrate Controller 我的迁移控制器

 public function migrate(BackupUser $buser, User $user)
            {
                $backup_user = $buser->get();

                foreach ($backup_user as $bulk) {
                    $user->create([
                        'id'         => $bulk->uye_id,
                        'name'       => $bulk->uye_nick,
                        'email'      => $bulk->uye_mail,
                        'password'   => $bulk->uye_sifre,
                        'created_at' => $bulk->uye_tarih
                    ]);
                }
            }

when I do this, the AUTOINCREMENT increases normally and I can not get the ID information of the previous members. 这样做时,AUTOINCREMENT通常会增加,而我无法获取以前成员的ID信息。

What is the best way to do this? 做这个的最好方式是什么?

I think you are trying to take database from joomla or wordpress to laravel. 我认为您正在尝试将数据库从joomla或wordpress迁移到laravel。

If I need to do this . 如果我需要这样做。 my approach would be to store first all base tables without id and then with stored id generated after insertion would be in related tables. 我的方法是先存储所有不带id基本表,然后将插入后生成的具有存储id的存储在相关表中。 So the steps would be 所以步骤是

  S1:  store new `user` and get `id`
  S2:  relate with other table like `role` with `user_id`
  Loop again till last row

By default, the id field for User is not fillable by mass assignment like you're doing. 默认情况下, Userid字段不能像您正在执行的那样通过批量分配填充。 If you want to assign it, you must add 'id' to the array of fillable fields in the user class. 如果要分配它,则必须在用户类的可填充字段数组中添加'id'

app/User.php app / User.php

...
class User extends Authenticatable
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'id', 'name', 'email', 'password',
    ];
...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM