简体   繁体   English

Laravel Nova 2型号在1资源中

[英]Laravel Nova 2 models in 1 resource

In my case i have a User 在我的情况下,我有一个用户

 Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('address_id')->nullable();
        $table->foreign('address_id')->references('id')->on('addresses');
        $table->string('first_name');
        $table->string('insertion')->nullable();
        $table->string('last_name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->string('phone_number');
        $table->string('avatar')->nullable();
        $table->rememberToken();
        $table->timestamps();
        $table->softDeletes();
    });

And i have a Address 我有一个地址

Schema::create('addresses', function (Blueprint $table) {
        $table->increments('id');
        $table->string('city');
        $table->string('street');
        $table->string('zip_code');
        $table->string('state');
        $table->string('house_number');
        $table->timestamps();
    });

Is it possible to create a new user and add a address to it in the same Nova User Resource? 是否可以在同一个Nova用户资源中创建新用户并为其添加地址?

What can we add in the fields function of the Resource? 我们可以在资源的字段功能中添加什么?

Do i need to overwrite the default create method? 我是否需要覆盖默认的create方法? Or can this be fixed with a belongsTo ? 或者可以用belongsTo修复?

Currently i got it to work with using 目前我得到了使用它

morhpOne morhpOne

method but this is not what i want. 方法,但这不是我想要的。 I first have to create a User and after that i can add an Address. 我首先要创建一个用户,然后我可以添加一个地址。

This should be simple, in your Nova User model add 这应该很简单,在Nova User模型中添加

BelongsTo::make('Address');

The Address field should represent the function in your User model that returns the Address relation Address字段应表示User模型中返回Address关系的函数

So in your user there should be 所以在你的用户应该有

public function address()
{
    return $this->belongsTo(Address::class);
}

I hope that that is what you are looking for, I will be happy to update my answer if given more information. 我希望这就是您所寻找的,如果有更多信息,我将很乐意更新我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM