Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

404
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!