【问题标题】:Laravel Replicate Certain Columns to Another TableLaravel 将某些列复制到另一个表
【发布时间】:2021-05-03 09:49:27
【问题描述】:

我想将某些列复制到另一个表。

foreach ($student->father_registrars as $getFatherData) {
            $fatherID = $getFatherData->id;
          
            // PROBLEM LAYS HERE   
            $fatherData =  \App\FatherRegistrars::where('id', $fatherID)->firstOrFail()();
            $fatherData->makeHidden(['birth', 'religion', 'education', 'citizenship', 'blood', 'id_card', 'residence_province', 'residence_regency', 'residence_district', 'residence_village', 'residence_address', 'residence_kode_pos', 'company_name', 'salary', 'company_address', 'company_province', 'company_regency', 'company_district', 'company_village', 'company_kode_pos']);
            
            $replicaFatherData = $fatherData->replicate();
            $fatherDatatoArray = $replicaFatherData->toArray();

            $father = \App\User::firstOrCreate($fatherDatatoArray);

            if ($isStudentExist >= 1) {
                $father->assignRole(['Parent', 'Guardian']);
            } else {
                $father->assignRole('Parent');
            }

            $father->password = $fatherData->password;

            $father->save();

            
        }

如您所见,我排除了birthreligion 等字段。但仍然给我一个错误Column not found: 1054 Unknown column 'birth' in 'where clause' (SQL: select * from users where...

当我尝试在没有foreach 的情况下运行相同的以下代码时,有些奇怪,但它可以工作!

$find_one = \App\StudentRegistrars::where('id', $id)->firstOrFail();
        $find_one->makeHidden(['state', 'status', 'id', 'email_sent', 'address', 'provinces', 'regencies', 'districts', 'villages', 'kode_pos', 'nickname', 'nik', 'religion', 'citizenship', 'sequence', 'weight', 'height', 'blood', 'date', 'photo', 'family_card', 'birth_certificate', 'passed']);
        $new_user = $find_one->replicate();
        $new_user = $find_one->toArray();

        $user = \App\User::firstOrCreate($new_user);
        $user->assignRole('Student Registrars');
        $user->password = $find_one->password;
        $user->save();

【问题讨论】:

  • 您在 ->first() 上运行查询。所以错误已经被抛出。这回答了你的问题了吗? stackoverflow.com/questions/23612221/…(excludeScope 部分)
  • 我已经尝试过那个解决方案,但仍然不起作用
  • 我已经更新了问题。请检查!
  • 你正在使用firstOrFail(),所以如果它失败了,那么makeHidden() 将不起作用并引发Column not found: 1054 Unknown column…
  • firstOrFail()(); 也去掉双括号

标签: laravel eloquent


【解决方案1】:

如果您知道要在两个模型之间保留哪些字段:

$fatherOrigin = \App\FatherRegistrars::where('id', $fatherID)->first();
$fieldsToKeep = [ 'name', 'email', ... etc ... ];

$father = new \App\User(); 
foreach($fieldsToKeep as $field) {
   $father->$field = $fatherOrigin->$field;
}

如果您知道要丢弃的字段:

$fatherOrigin = \App\FatherRegistrars::where('id', $fatherID)->first();
$fieldsToDiscard = [ 'birth', 'religion', ... etc ... ];
$originData = $fatherOrigin->toArray();

foreach($fieldsToDiscard as $field) {
   unset($originData[$field]);
}

$father = \App\User::firstOrCreate($originData); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    • 2017-11-02
    相关资源
    最近更新 更多