Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel将某些列复制到另一个表中_Laravel_Eloquent - Fatal编程技术网

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
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();

如果您知道要在两个模型之间保留哪些字段,请执行以下操作:

$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();
也要去掉双括号