我有两张桌子:
表1(缔约方):
public function up()
{
Schema::create('parties', function (Blueprint $table) {
$table->id();
$table->string('full_name');
$table->string('ic_passport');
$table->string('nationality');
$table->string('income_tax_no');
$table->string('income_Tax_filing_branch');
$table->string('phone_no');
$table->string('email');
$table->timestamps();
});
}表二(长裙):
public function up()
{
Schema::create('corraddresses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('party_id');
$table->foreign('party_id')->references('id')->on('parties');
$table->string('address_1');
$table->string('address_2');
$table->string('city');
$table->string('poscode');
$table->timestamps();
});
}模式一(缔约方):
class Party extends Model
{
use HasFactory;
protected $fillable = ['full_name','ic_passport','nationality','income_tax_no',
'income_Tax_filing_branch','phone_no','email'];
}第二种模式(护肤品):
class Corraddress extends Model
{
use HasFactory;
protected $fillable = ['address_1','address_2','city','poscode'];
}控制器一(PartyController-存储功能):
public function store(Request $request)
{
$party = Party::create($request->post());
return response()->json([
'message'=>'Party Created Successfully!!',
'party'=>$party
]);
}控制器二(CorraddressController-存储功能):
public function store(Request $request)
{
$corraddress = Corraddress::create($request->post());
return response()->json([
'message'=>'Corraddress Created Successfully!!',
'corraddress'=>$corraddress
]);
}在我看来,提交一份表格只是为了将数据放在两个不同的表格中,所以我在提交表格“各方”时所要做的事情将被张贴,然后“紧身裙”将被张贴,并将“各方”中的主键作为外键分配给“紧身裙”。
意见如下:
data() {
return {
full_name: '',
ic_passport: '',
nationality: '',
income_tax_no: '',
income_Tax_filing_branch: '',
phone_no: '',
email: '',
address_1: '',
address_2: '',
city: '',
poscode: '',
};
},
methods: {
addNewPost(){
axios.post('/api/auth/party',{
full_name: this.full_name,
ic_passport: this.ic_passport,
nationality: this.nationality,
income_tax_no: this.income_tax_no,
income_Tax_filing_branch: this.income_Tax_filing_branch,
phone_no: this.phone_no,
email: this.email,
})
axios.post('/api/auth/corraddress',{
address_1: this.address_1,
address_2: this.address_2,
city: this.city,
poscode: this.poscode,
})
.then((response)=>{
this.$router.push({name:"database-management"})
$('#success').html(response.data.message)
})
},
},因此,在上述情况下,“派对”的数据将被发布,但它不会发布任何关于“紧身裙”的信息。

但是,当我试图在不分配外键的情况下发布表单时,它将在两个表中发布。
我关于外键的方法有问题吗?或者我没有做的任何额外步骤?
发布于 2022-11-04 07:52:42
您将收到500个错误,因为列party_id不能为空,因此插入数据库失败。在创建新的紧身连衣裙时,您总是必须发送它,或者您可以将其设置为可空。
$table->unsignedBigInteger('party_id')->nullable();我建议你用一种更优雅的方式来定义异域身份:
$table->foreignId('party_id')
->nullable()
->constrained('parties')
->cascadeOnDelete()
->cascadeOnUpdate();另一个建议是在.env文件中设置
APP_DEBUG=true当发生错误时,从laravel收到更多详细信息。
编辑:
axios.post('/api/auth/party',{
full_name: this.full_name,
ic_passport: this.ic_passport,
nationality: this.nationality,
income_tax_no: this.income_tax_no,
income_Tax_filing_branch: this.income_Tax_filing_branch,
phone_no: this.phone_no,
email: this.email,
}).then(response => {
axios.post('/api/auth/corraddress',{
address_1: this.address_1,
address_2: this.address_2,
city: this.city,
poscode: this.poscode,
party_id: response.party.id
})
}抱歉,我对axios还不习惯
https://stackoverflow.com/questions/74313463
复制相似问题