Search code examples
laravellaravel-nova

Laravel Nova Custom Fields get model value


I created a custom field for Nova. On this field I need to get some extra model data like $model->country to show on the form. How can I pass this data to the Vue component?

I try to use:

return $this->withMeta

But I don't know how to pass the data from the model.


Solution

  • Add the custom field like a normal field to the resource fields() and chain a custom method with the data from the model:

    CountryField::make('Country')->country('Germany'),
    

    Define this custom method in your Nova component (see src folder):

    public function country($value)
    {
        return $this->withMeta([
            'country' => $value,
        ]);
    }
    

    You can access the returned data from this method in FormField.vue like this:

    {{ field.country }}