【发布时间】: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();
}
如您所见,我排除了birth、religion 等字段。但仍然给我一个错误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()();也去掉双括号