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.
]),