将输入字段保存在Laravel中的另一个表中

所以我现在所拥有的: 一个CRUD应用程序:一个注册表和一个表格,以及一个Extraregistration表,我要在其中保存注册中的某些字段。 Extraregistration表具有一个registration_id。

RegistrationController

$registration->ris_firstname = $request->firstname;
$registration->ris_lastname= $request->lastname;
$registration->ris_email = $request->email;

html

<label>Firstname</label>
<input type="text" name="firstname" id="firstname" class="form-control" data-blocked="<>{}" value="{{ ($cache['firstname'] ?? old('firstname') ?? $registration->ris_firstname ?? '') }}"  required>

<label>Lastname</label>
<input type="text" name="lastname" id="lastname" class="form-control" data-blocked="<>{}" value="{{ ($cache['lastname'] ?? old('lastname') ?? $registration->ris_lastname ?? '') }}"  required>

<label>E-mail</label>
<input type="text" name="email" id="email" class="form-control" data-blocked="<>{}" value="{{ ($cache['email'] ?? old('email') ?? $registration->ris_email ?? '') }}"  required>

ExtraregistrationController

$extraregistration->iea_price = $request->price;
$extraextraregistration->iea_registration_id = $registration_id;

html

<label>Price</label>
<input type="text" name="price" id="price" class="form-control" data-blocked="<>{}" value="{{ ($cache['price'] ?? old('price') ?? $extraregistration->iea_price ?? '') }}"  required>

我想将名字和姓氏保存到Extraregistration表中,而不是在普通注册表中。 因此,当我添加注册时,名字和姓氏转到数据库中Extraregistration表中的registration_firstname字段中的extraregistration表中 预先感谢!

cengyicang 回答:将输入字段保存在Laravel中的另一个表中

在RegisterController中的创建函数中。

$extraregistration = new YourModel();
$extraregistration->ris_firstname = $request->firstname;
$extraregistration->ris_lastname= $request->lastname;
$extraregistration->save();
,

您需要首先更改数据库结构。 然后

注册用户。保存后,该对象将具有“ id”

$registration = new Registration();
$registration->ris_email = $request->email;
$registration->save();

然后创建额外的用户数据对象,并使用注册对象中的ID。

$extraregistration = new ExtraRegistration();
$extraregistration->ris_firstname = $request->firstname;
$extraregistration->ris_lastname= $request->lastname;
$extraregistration->iea_price = $request->price;
$extraextraregistration->iea_registration_id = $registration->id;
$extraregistration->save();
,

如果数据库中有一个字段,则需要将名字和姓氏直接存储到Extraregistration表中。 (额外注册表)。

$extraregistration = new Extraregistration();
$extraregistration->registration_firstname = $request->firstname;
$extraregistration->registration_lastname = $request->lastname;
$extraregistration->save();

OR

如果您在Extraregistration表中没有专门的字段,请像foreign key那样使用registration_id

存储注册数据时,您需要将新创建的registration_id存储到 Extraregistration表如下:

$registration = new Registration();
$registration->ris_email = $request->email;
$registration->save();

$extraregistration->iea_price = $request->price;
$extraextraregistration->iea_registration_id = $registration->id;
$extraregistration->save();
本文链接:https://www.f2er.com/3167885.html

大家都在问