Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

otrsw's avatar
Level 5

Laravel Nova: Bind Fields to JSON attribute in model dynamically

For a particular project, I would like to use the power of the Nova Resource UI combined with a more flexible data model. Specifically, I want to be able to add fields to the resource for attributes stored inside a JSON database field, and not on the table.

Specifics:

Database model: quotations (migration included)

public function up()
{
    Schema::create('quotations', function(Blueprint $table)
    {
        $table->bigInteger('id', true);
        $table->timestamps();
        $table->string('client_name', 100)->nullable();
        $table->string('client_surname', 100)->nullable();
        $table->string('status', 10)->nullable()->default('NEW');
        $table->text('data')->nullable();
    });
}

Nova Resource

So I can define a "normal" NOVA resource and define the following fields (*ignoring status) in App\Nova\Quotation:

public function fields(Request $request)
{
    return [
        Text::make('Client Name')->sortable(),
        Text::make('Client Surname')->sortable(),


    ];
}

Now my "wish" is to have something to this effect, using the non-existant "bindTo" method to illustrate what I want to achieve

public function fields(Request $request)
{
    return [
        Text::make('Client Name')->sortable(),
        Text::make('Client Surname')->sortable(),
    
        //Fields bound into the JSON data property  
        Text::make('Client Id')->bindTo('data.client_id'),
        Date::make('Client Date Of Birth')->bindTo('data.client_date_of_birth'),

        //etc       


    ];
}

So when a Quotation model is saved, the client_name and client_surname attributes will save to the database as per normal. but client_id and client_date_of_birth should save to the JSON data attribute.

I know I can set up a Mutator on the Quotation model

    public function setClientIdAttribute($value)
    {
            set_data($this->data,'client_id',$value);
    }

However, that would still require a "non-dynamic" Quotation model. I want to be able to add fields to the view dynamically without having to change the Quotation model beyond the basics. A real world example would be where different products have different input fields to gather before generating a quote. I could then easily inject the field definition dynamically in the Nova Resource whilst keeping the database model simple.

I would love to get some input on how to tackle this requirement.

0 likes
1 reply
yaroslawww's avatar

You should use model setters ans getters like:


public function getShortDescriptionAttribute()
{
        return $this->meta->short_description;
}

public function setShortDescriptionAttribute($values)
{
        $this->meta->short_description = $values;
}

1 like

Please or to participate in this conversation.