我正在尝试将长格式数据保存到数据库。直到现在,我从请求对象获取表单值并将其设置为数据库模型。 这工作得很好。但是我想知道是否还有另一种方法可以有效地初始化模型而无需设置每个值。我的模型具有一对一的关系。
我一直在这样。但是我认为这不是正确的方法
//Student details
$studentDetail->student_first_name = $request->input('studentFirstName');
$studentDetail->student_last_name = $request->input('studentLastName');
$studentDetail->student_phone_number = $request->input('studentPhoneNumber');
$studentDetail->student_date_of_birth = $request->input('studentDOB');
$studentDetail->student_email = $request->input('studentEmail');
$studentDetail->save();
$studentAddress = new Address();
$studentAddress->address_1 = $request->input('studentAddress1');
$studentAddress->address_2 = $request->input('studentAddress2');
$studentAddress->city = $request->input('studentCity');
$studentAddress->state = $request->input('studentState');
$studentAddress->country = $request->input('studentCountry');
$studentAddress->post_code = $request->input('studentPostCode');
$studentDetail->addresses()->save($studentAddress);
$visaDetails = new Visa();
$visaDetails->passport_number = $request->input("visaPassportNumber");
$visaDetails->visa_number = $request->input("visaVisaNumber");
$visaDetails->visa_class = $request->input("visaVisaClass");
$visaDetails->visa_grant_date = $request->input("visaVisaGrantDate");
$visaDetails->visa_expiry_date = $request->input("visaVisaExpiryDate");
$studentDetail->visaDetails()->save($visaDetails);
//
$instituteDetails = new Institute();
$instituteDetails->institute_name = $request->input("instituteName");
$instituteDetails->institute_location = $request->input("instituteLocation");
$instituteDetails->institute_phone1 = $request->input("institutePhone1");
$instituteDetails->institute_phone2 = $request->input("institutePhone2");
$instituteDetails->institute_email = $request->input("instituteEmail");
// dd($instituteDetails->courses);
$courseDetails = new Course();
$courseDetails->course_level = $request->input("courseLevel");
$courseDetails->course_name = $request->input("courseName");
$courseDetails->course_fee = $request->input("courseFee");
$courseDetails->course_concession_fee = $request->input("courseConcessionFee");
$courseDetails->course_duration = $request->input("courseDuration");
$courseDetails->course_commencement_date = $request->input("courseCommencementDate");
$studentDetail->instituteDetails()->save($instituteDetails);
$instituteDetails->courses()->save($courseDetails);
关于加快此过程的任何想法吗?
答案 0 :(得分:1)
只需设置使用批量分配即可创建模型,因此:
因此在您的模型中 StudentDetail :
class StudentDetail{
protected $fillable = [
'student_first_name',
'student_last_name',
'student_phone_number',
'student_date_of_birth',
'student_email',
];
//...
//... rest of your model
}
然后调整HTML输入,使其用户名称具有名称,例如:
<input type="text" id="foo" name="student[student_first_name]">
<input type="text" id="foo" name="student[student_last_name]">
.....
提示:为了进行验证,您必须使用点表示法对其进行处理,因此您的规则可能是:
'student.student_first_name' => 'required|humanName|string|max:255',
现在只需在控制器中执行以下操作即可
:$studentDetail = StudentDetail::create($request->input('student'));
现在,您对地址和其他模型进行了相同的操作。
GIST::为模型启用批量分配后,您可能最终只有 ON 以下几行代码为您完成了这一切,而且更加有趣,充满活力;)恕我直言!
$relatedModels = ['Address', 'Visa', 'Institute', 'Course'];
foreach ($relatedModels as $relatedModel) {
$relatedModelClass = 'App\\'.$relatedModel; //adjust the namespace of your models here.
$ormRelatedModel = $relatedModelClass::create(strtolower($request->input($relatedModel)));
$studentDetail->{strtolower(str_plural($relatedModel)) . 'Details'}()->save($ormRelatedModel);
}
请注意,在这种情况下,应将您的关系名称更改为类似于StudentDetail类/模型中的addresses
函数,或将其更改为addressesDetails,或者从上面的示例代码中删除.'Details'
,然后将其从其他关系名称中删除,即:将instituteDetails()
更改为institute()
。并将关系名称复数!
我刚刚对其进行了测试,并且可以正常工作, 干杯!