Laravel将某些列复制到另一个表中
我想将某些列复制到另一个表中Laravel将某些列复制到另一个表中,laravel,eloquent,Laravel,Eloquent,我想将某些列复制到另一个表中 foreach ($student->father_registrars as $getFatherData) { $fatherID = $getFatherData->id; // PROBLEM LAYS HERE $fatherData = \App\FatherRegistrars::where('id', $fatherID)->fir
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();
}
如您所见,我排除了出生
,宗教
等字段。。但是仍然给了我一个错误未找到列:1054“where子句”(SQL:select*from
userswhere…
当我尝试在没有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();
如果您知道要在两个模型之间保留哪些字段,请执行以下操作:
$fatherOrigin=\App\fatherregisters::where('id',$fatherID)->first();
$FIELDSTOKEP=[“姓名”、“电子邮件”等];
$father=new\App\User();
foreach($fieldsToKeep作为$field){
$father->$field=$fatherOrigin->$field;
}
如果您知道要放弃哪个字段:
$fatherOrigin=\App\fatherregisters::where('id',$fatherID)->first();
$fieldsToDiscard=[“出生”、“宗教”等];
$originData=$fatherOrigin->toArray();
foreach($fieldsToDiscard作为$field){
未设置($originData[$field]);
}
$father=\App\User::firstOrCreate($originData);
您在->first()上运行查询。因此,抛出的错误已准备就绪。这是否回答了您的问题?(excludeScope部分)我尝试了这一解决方案,但仍然不起作用我已更新了问题。请检查!您正在使用firstOrFail()
,因此如果失败,则使用makeHidden()
不起作用,提升列未找到:1054未知列…
firstOrFail();
也要去掉双括号