简体   繁体   中英

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?

What can we add in the fields function of the Resource?

Do i need to overwrite the default create method? Or can this be fixed with a belongsTo ?

Currently i got it to work with using

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

BelongsTo::make('Address');

The Address field should represent the function in your User model that returns the Address relation

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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