Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

401
Vistas
Use relation (for ex. belongsTo) inside KeyValue field in Laravel Nova

Laravel: 8.51 Nova: 3.24 PHP Version: 7.4.20

I have problem with trying to solve this situation:

I have a action that uses a KeyValue to handle array of pairs: "product_id" and "amount" field looks like:

KeyValue::make(__('Order details'), 'details')
            ->rules('json'),

also in model i added cast:

protected $casts = [
    'details' => 'array',
];

I convert it to json and store in database like that

foreach ($fields->details as $key => $value)
{
    $order_details[$key] = $value;
}
//(...)
DB::table('orders')->InsertGetId([
    'details' => json_encode($order_details),
]);

finnaly my json field in database looks like:

{"1":"6","2":"7","3":"8"}

To this point everything works great.

Now I want to change what I see as product_id (in above example 1, 2, 3) to be from other resource like belongsTo('products')

I was considering how to be able to use BelongsTo or Select in key part of KeyValue field. I tried few ways to display it somehow in stack field like:

Stack::make('Details', [
    Text::make('details', function () {
        return $this->details($key); // how to properly read $key from json in fields?
    }),
    Text::make('amount',function () {
        return $this->details($value); // how to properly read $value from json in fields?
    }),
]),

or

BelongsTo::make(__('Product'), 'user', Products::class)
            ->withMeta([
                'belongsToId' => $this->details($key), // again i dont know how to properly read $key here from json.
                ]),
over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda