2

Laravel:8.51 Nova:3.24 PHP 版本:7.4.20

我在尝试解决这种情况时遇到问题:

我有一个使用 KeyValue 来处理对数组的操作:“product_id”和“amount”字段如下所示:

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

同样在模型中我添加了演员:

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

我将它转换为json并像这样存储在数据库中

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

最后我在数据库中的 json 字段看起来像:

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

到目前为止,一切都很好。

现在我想将我看到的 product_id (在上面的示例 1、2、3 中)更改为来自其他资源,例如 belongsTo('products')

我正在考虑如何在 KeyValue 字段的关键部分使用 BelongsTo 或 Select。我尝试了几种方法以某种方式在堆栈字段中显示它,例如:

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?
    }),
]),

或者

BelongsTo::make(__('Product'), 'user', Products::class)
            ->withMeta([
                'belongsToId' => $this->details($key), // again i dont know how to properly read $key here from json.
                ]),
4

0 回答 0