首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Laravel vue提交表格时,外键不会张贴

在Laravel vue提交表格时,外键不会张贴
EN

Stack Overflow用户
提问于 2022-11-04 07:21:26
回答 1查看 34关注 0票数 0

我有两张桌子:

表1(缔约方):

代码语言:javascript
复制
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();
    });
}

表二(长裙):

代码语言:javascript
复制
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();
    });
}

模式一(缔约方):

代码语言:javascript
复制
class Party extends Model
{
use HasFactory;
protected $fillable = ['full_name','ic_passport','nationality','income_tax_no',
'income_Tax_filing_branch','phone_no','email'];
}

第二种模式(护肤品):

代码语言:javascript
复制
class Corraddress extends Model
{
use HasFactory;
protected $fillable = ['address_1','address_2','city','poscode'];
}

控制器一(PartyController-存储功能):

代码语言:javascript
复制
public function store(Request $request)
{
    $party = Party::create($request->post());
    return response()->json([
        'message'=>'Party Created Successfully!!',
        'party'=>$party
    ]);
}

控制器二(CorraddressController-存储功能):

代码语言:javascript
复制
public function store(Request $request)
{
    $corraddress = Corraddress::create($request->post());
    return response()->json([
        'message'=>'Corraddress Created Successfully!!',
        'corraddress'=>$corraddress
    ]);
}

在我看来,提交一份表格只是为了将数据放在两个不同的表格中,所以我在提交表格“各方”时所要做的事情将被张贴,然后“紧身裙”将被张贴,并将“各方”中的主键作为外键分配给“紧身裙”。

意见如下:

代码语言:javascript
复制
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)
            })
},
},

因此,在上述情况下,“派对”的数据将被发布,但它不会发布任何关于“紧身裙”的信息。

但是,当我试图在不分配外键的情况下发布表单时,它将在两个表中发布。

我关于外键的方法有问题吗?或者我没有做的任何额外步骤?

EN

回答 1

Stack Overflow用户

发布于 2022-11-04 07:52:42

您将收到500个错误,因为列party_id不能为空,因此插入数据库失败。在创建新的紧身连衣裙时,您总是必须发送它,或者您可以将其设置为可空。

代码语言:javascript
复制
$table->unsignedBigInteger('party_id')->nullable();

我建议你用一种更优雅的方式来定义异域身份:

代码语言:javascript
复制
$table->foreignId('party_id')
                ->nullable()
                ->constrained('parties')
                ->cascadeOnDelete()
                ->cascadeOnUpdate();

另一个建议是在.env文件中设置

代码语言:javascript
复制
APP_DEBUG=true

当发生错误时,从laravel收到更多详细信息。

编辑:

代码语言:javascript
复制
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还不习惯

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74313463

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档