简体   繁体   English

Laravel Backpack不更新JSON字段

[英]Laravel Backpack not updating JSON field

I have a TEXT Column in my MySQL table, which I want to fill with a JSON object as string so I can define some options for the entry I am saving per row. 我在我的MySQL表中有一个TEXT列,我希望用一个JSON对象填充为字符串,这样我就可以为每行保存的条目定义一些选项。

I only have one option yet, so I show only one checkbox, with a value of 1 . 我只有一个选项,所以我只显示一个复选框,值为1 Now when the form is submitted, I validate the input via the request. 现在提交表单时,我通过请求验证输入。 To make sure that there is always a value available I check if the input exists (remember it's a checkbox) and if not, I prefill it with the option set to 0 . 为了确保总有一个值可用,我检查输入是否存在(记住它是一个复选框),如果没有,我预先填充选项设置为0 But when I take a look at phpMyAdmin the old value remains. 但是当我看一下phpMyAdmin时,旧值仍然存在。

Here the example by code - I was never good in explaining my self in englisch :P 这里的代码示例 - 我从来没有善于在englisch中解释我的自我:P

Form 形成

    <label for="dedication" style="font-weight: normal;"><input id="dedication" type="checkbox" name="fields[dedication]" value="1" />&nbsp;Dedication</label>

Request::all() overwrite Request :: all()覆盖

public function all() {

    // get input
    $input = parent::all();

    // save empty array if fields not set
    if (!isset($input['fields'])) {
        $input['fields'] = array("dedication" => "0");
    }

    $this->replace($input);

    // dd(parent::all());

    return parent::all();
}

Model 模型

protected $table = 'products';
protected $primaryKey = 'id';
// public $timestamps = false;
protected $guarded = ['id'];
protected $fillable = ['product', 'description', 'price', 'campaign_id', 'quantity', 'fields'];
protected $hidden = ['created_at', 'updated_at'];
// protected $dates = [];
protected $casts = ['fields' => 'array'];

Example

Lets say I create a Product, and don't check the dedication checkbox, then the Product is saved with a value of NULL in the fields column. 假设我创建了一个Product,并且没有选中dedication复选框,那么在fields列中保存Product为NULL值。 Wanted is {"dedication": "0"} . 通缉是{"dedication": "0"}

When I now update this entry, and check the dedication checkbox, it works. 当我现在更新此条目,并检查dedication复选框时,它可以正常工作。 Value is {"dedication": "1"} . 价值是{"dedication": "1"}

Now, if I update the entry again, and leafe the checkbox unchecked, the fields column is not overwritten so the old value remains. 现在,如果我再次更新该条目,并且未选中该复选框,则不会覆盖fields列,因此旧值仍然存在。

How can I update the values of this column? 如何更新此列的值?

So I found out, that the update method on my crud controller doesn't pass the updated request to the updateCrud method of the CrudController . 于是我发现,该update我的CRUD控制器上的方法没有通过更新请求updateCrud的方法CrudController All I had to do, was to pass the updated request. 我所要做的就是传递更新的请求。

My update method in the ProductCrudController now looks like this: 我在ProductCrudController update方法现在如下所示:

public function update(UpdateRequest $request)
{
    // your additional operations before save here
    $redirect_location = parent::updateCrud($request);
    // your additional operations after save here
    // use $this->data['entry'] or $this->crud->entry
    return $redirect_location;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM