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

anditsung's avatar

Model with BelongsTo and BelongsToMany

i have a Resource with this fields

public function fields(Request $request) { return [ BelongsTo::make('Material'),

        BelongsToMany::make('Materials')
            ->fields(function() {
                return [
                    Number::make('Quantity'),

                    Boolean::make('Need Test'),
                ];
            })
    ];
}

MODEL public function material() : BelongsTo { return $this->belongsTo(Material::class); }

public function materials() : BelongsToMany
{
    return $this->belongsToMany(Material::class, 'warehouse_chemical_formula_materials');
}

when i try to attach material i only get 1 input for material selection i cant add other info like quantity and need test.

when i remove BelongsTo::make('Material'),

i can add that field. anyone know how to fix this issue?

0 likes
4 replies
wilburpowery's avatar

Why are you using both the BelongsTo and BelongsToMany Nova fields?

You should only use the BelongsToMany since that's the relationship you're trying to work with.

anditsung's avatar

im trying to create a new material from other material belongsto -> to new material belongstomany -> what material to be use to create new material

wilburpowery's avatar

You would need to create a new Material resource and create it from there?

anditsung's avatar

before using poly i thought about using this kind of approach.

right now i test another approach using poly new material be create on the formula model then raw material can be list on the formula.

compound = [ id, name, compound_type_id, plant_id, product_type_id ] premix = [ id, name, material_id ] dispersion = [ id, name, material_id ] extra = [ id, name, plant_id ]

formula model

public function formulaable()
{
    return $this->morphTo();
}

compound, premix, dispersion and extra model

public function formula()
{
    return $this->morphOne(Formula::class, 'formulaable')
}

hopefully this is the right approach for this situasion

Please or to participate in this conversation.