Search code examples
phplaravellaravel-nova

Laravel Nova HasOne Relationship view and edit 2 models in single panel


I have a HasOne relationship between 2 models.

In Laravel Nova, is it possible to merge them into a single panel when viewing details, and update both model together when updating details.


Solution

  • This feature is not officially supported in laravel nova. but there is a workaround to achieve it using model accessors and mutators. this answer can help you

    Example:

    // in the model
    public function getProfilePictureAttribute()
    {
       // Don't forget to check the relation here if you don't want any error on Nova.
       return $this->profile ? $this->profile->picture : null;
    }
    
    public function setProfilePictureAttribute($value)
    {
       $profile = $this->profile;
       $profile->fill(['picture' => $value]);
       $profile->save();
    }
    // in the resource
    Image::make('Profile picture')