Php 与laravel中的其他列一起更新json列

Php 与laravel中的其他列一起更新json列,php,json,laravel,eloquent,Php,Json,Laravel,Eloquent,我想知道当其他列也需要更新时,如何更新JSON列数据 正如我在laravel网站上看到的示例,json专栏的内容是这样的 DB::table('users') ->where('id', 1) ->update(['options->enabled' => true]); 但是其他专栏呢 以下是我目前的情况: public function update(Request $request, $id) {

我想知道当其他列也需要更新时,如何更新JSON列数据

正如我在laravel网站上看到的示例,json专栏的内容是这样的

DB::table('users')
            ->where('id', 1)
            ->update(['options->enabled' => true]);
但是其他专栏呢

以下是我目前的情况:

public function update(Request $request, $id)
    {
        $order = Order::find($id);
        $this->validate($request, array(
            'payment_id'=>'nullable',
            'orderstatus_id' => 'required|numeric',
            'address_id' => 'required|numeric',
        ));

        $order = Order::where('id',$id)->first();
        $order->payment_id = $request->input('payment_id');
        $order->orderstatus_id = $request->input('orderstatus_id');
        $order->address_id = $request->input('address_id');

        $order->save();

        return redirect()->route('orders.index',
            $order->id)->with('success',
            'Order updated successfully.');
    }
我有一个名为
product\u name
的列,这是一个Json类型的列,在其中我有一些数据,比如下面的示例,我想在其中编辑产品的
quantity

{"29": {"id": 29, "name": "effewf", "price": 24524, "quantity": 3, "attributes": [], "conditions": []}, "37": {"id": 37, "name": "test product", "price": 456346, "quantity": 2, "attributes": [], "conditions": []}}

正如您在订单中看到的,我有两种产品,一种数量为
3
,另一种数量为
2
,处理此问题的最佳方法是为这些数据创建一个模型和一个表

但如果要使用JSON,可以使用模型中的属性:

protected $casts = [
    'product_name' => 'array',
];
然后,您将能够像使用阵列一样使用选项:

$order = Order::where('id',$id)->first();
$order->payment_id = $request->input('payment_id');
$order->orderstatus_id = $request->input('orderstatus_id');
$order->address_id = $request->input('address_id');
$order->product_name[$request->product_id]['quantity'] = $request->new_product_quantity);
$order->save();
如果不想使用强制转换,可以手动转换数据:

$order->product_name = json_encode(
    json_decode($order->product_name, true)[$request->some_product_id]['quantity'] = $request->new_quantity
);

Laravel文档中有一节介绍。方法是将
$casts
属性添加到您的
订单
模型中:

protected $casts = [
    'product_name' => 'array',
];
在刀片式视图和控制器中,您应该能够直接访问
$order->product\u name
,无需再执行任何
json\u decode()
调用

更新特定值时,在“编辑顺序”视图中,您可能希望将表单输入命名为类似的名称,以便于映射更新,即类似于:

@foreach($order->product_name as $data) 
    <label>Name:</label>
    <input
      type="text"
      name="product_name[{{ $data['id'] }}][name]"
      value="{{ $data['name'] }}"
    >

    <label>Price:</label>
    <input
      type="text"
      name="product_name[{{ $data['id'] }}][price]"
      value="{{ $data['price'] }}"
    >

    <label>Quantity:</label>
    <input
      type="number"
      name="product_name[{{ $data['id'] }}][quantity]"
      value="{{ $data['quantity'] }}"
    >
@endforeach

当我使用
json\u decode
来获取数据时,如果我在我的模型中使用
casts
,我会得到
json\u decode()的错误,我希望参数1是字符串,数组给定
,但是如果除了
“product\u name”=>“array”之外,
我使用
“product\u name”=>,
我仍然可以得到没有错误的结果,使用
empty
而不是数组进行强制转换会有问题吗?@mafortis
casts
将自动将JSON转换为数组,然后将数组转换为JSON,然后保存到DB。因此,您不需要使用
json\u decode()
json\u encode()
不,您没有理解我的意思。问题是,如果我在模型中添加该数组,我必须更改所有函数和刀片代码,以重新获得当前获得的结果。因此,使用
'
而不是
'array'
是一个问题吗?@mafortis如果您当前在任何地方都使用
json_decode()
,请不要使用
casts
并手动执行
json_编码(json_decode($order->product_name),true)[$request->some_product_id]['quantity']=$request->new u quantity)
。但是使用
casts
要好得多。好的,你介意用你刚才给我的答案编辑你的代码吗?如果你能解释一下这个
$request->product\u id
&
新产品的数量
是从哪里来的,请告诉我?另外,我在blade中输入的用于验证的名称应该是什么?上面的代码可以工作,但当我保存数据时,除了更新值之外,它将添加另一个值,请查看数量
{“id”:29,“name”:“effewf”,“price”:24524,“quantity”:3,“quantity”,“quantity”,“1”,“attributes:[],“conditions:[]}
@mafortis在输入名称中错误地留下了单引号-在我的答案中删除了它们,现在应该可以用了。如果要确保价格和数量为整数值,则需要使用
type=“number”
输入,或者不在
$data
上执行循环,而是显式设置每个键,以便可以在需要时进行
int
-强制转换。
public function update(Request $request, $id)
{
    $order = Order::find($id);

    // ... other usual stuff

    // get original data
    $new_product_name = $order->product_name;

    // contains all the inputs from the form as an array
    $input_product_name = $request->input('product_name');
    // loop over the product array
    foreach ($input_product_name as $data_id => $data) {
        // loop over each input for the product in the view form
        foreach ($data as $key => $value) {
             // set the new value
             $new_product_name[$data_id][$key] = $value;
        }
    }
    // set the new data (should automatically serialize into JSON for storage)
    $order->product_name = $new_product_name;

    $order->save();
}