简体   繁体   English

如何根据laravel复选框值更新数据库

[英]How to update database base on laravel checkbox value

I load check boxes from database with they are checked or not,我从数据库中加载复选框,它们是否被选中,

<div class="row">
    <div class="col-xs-12">
        <div class="form-group">

            @php
                $json = $orders->data;
                $json = json_decode($json, true);
                $products = $json['order_info']['products'];

                $data = '';

                    foreach ($products as $hitsIndex => $hitsValue) {
                        $data .= $hitsValue['type']. ', ';
                    }
                $data = rtrim($data, ', ');

                $agosProducts = Utility::constant('agos_products1');
            @endphp

            {{Html::validation($orders, 'product')}}
            <label for="products" class="control-label">{{Translator::transSmart('app.Products', 'Products')}}</label><br><br>

            @foreach($agosProducts as $product)

                <label class="control-label "for="{{ $product['type'] }}">
                    <input id="{{ $product['type'] }}" name="{{ $product['type'] }}" type="checkbox" value="{{ $product['type'] }}"
                           @foreach ($products as $hitsIndex => $hitsValue)
                                @if(in_array($hitsValue['type'], $product)) checked=checked @endif
                           @endforeach
                    >
                    {{ $product['name'] }}
                </label>
                <br>

            @endforeach

        </div>
    </div>
</div>

Now i want to update my database base on checkbox value.现在我想根据复选框值更新我的数据库。

For example if say i load checkbox 1 as checked from database and now it's unchecked i need to update it in database.例如,如果说我从数据库中加载复选框 1,但现在未选中,我需要在数据库中更新它。

This code i can get all the current status of checkbox but i don't know previous values of this.这段代码我可以获得复选框的所有当前状态,但我不知道它以前的值。 So it hard update statue of current values and add new status in database,所以它很难更新当前值的状态并在数据库中添加新状态,

$chks = array('multicolor','resizable','showRuler','namesNumbersEnabled');
foreach ($chks as $chk) {
   $product->setAttribute($chk, (Input::has($chk)) ? true : false);
}

This is my json object save in data column这是我保存在数据列中的 json 对象

{"user_token":"ad48c412-3866-4ac9-adf6-3328911ae46c",
"order_info": 
{"order_id":"CGC12345678","company_id":32,"price":1000.5,"currency":"MYR",
"products":[
{"type":"HR_ECLAIM","name":"HREClaim","is_fixed_price":true,"price":500.5,"currency":"MYR"},
{"type":"HR_ELEAVE","name":"HRELeave","is_fixed_price":true,"price":500,"currency":"MYR"}
],
"total_invoices":200,"total_staffs":80},"url":"https://drive.google.com/open?id=1Is6QsnuMLu9ZIpqeEzR2O2Ve1wUyF92aVCg55kWsOgc"}

i load [order_info][products][type] as checked products in while i load all the products from env file.我加载 [order_info][products][type] 作为检查的产品,同时我从 env 文件加载所有产品。 I only need to save checked check box products in db.我只需要在数据库中保存选中的复选框产品。

Can someone helps me?有人可以帮助我吗?

This is the complete working solution i done for my question!这是我为我的问题所做的完整工作解决方案!

public function edit($id, $attributes){

        $orders = (new Agos())->load($id);
        $json = $orders->data;
        $json = json_decode($json);

        $checkedit= $attributes['check_list'];

        if (is_array($checkedit) || is_object($checkedit))
        {
            foreach($checkedit as $chked1){

                $exists = false;
                foreach($json->products as $key => $val)
                {
                    // update if products exists
                    if($val->type == $chked1) {
                        $val->status = 'true';
                        $exists = true;
                    }

                    if(array_key_exists('status', $val)) {}
                    else{ $val->status = 'false';}

                }

                if($chked1 == 'FIN_REPORTING')
                {
                    $name = 'Finance reporting & book keeping';
                }
                elseif ($chked1 == 'FIN_ADVISORY')
                {
                    $name = 'Finance Advisory';
                }
                elseif ($chked1 == 'PAYROLL_HRDB')
                {
                    $name = 'PAYROLL_HRDB';
                }
                elseif ($chked1 == 'HR_ELEAVE')
                {
                    $name =   'HR E-Leave';
                }
                else
                {
                    $name = 'HR E-Claim';
                }

                //if products not exists add new products
                if(!$exists) {

                    $newproduct = new \stdClass();
                    $newproduct->type = $chked1;
                    $newproduct->name = $name;
                    $newproduct->is_fixed_price = false;
                    $newproduct->currency = "MYR";
                    $newproduct->status = "true";
                    array_push($json->products, $newproduct);
                }
            }
        }

        //remove all products that have status = false
        foreach($json->products as $index => $product) {
            if ( $product->status == "false") {
                unset($json->products[$index]);
            }
        }
        $json->products = array_values($json->products);
        json_encode($json, JSON_PRETTY_PRINT);


        //remove status element from products
        foreach($json->products as $index => $product) {

                unset($product->status);

        }
        $json->products = array_values($json->products);
        json_encode($json, JSON_PRETTY_PRINT);

        $json->google_drive_url= $attributes['data'];
        $json->remark= $attributes['remark'];
        $json->status= $attributes['status'];
        $json->total_invoices= $attributes['total_invoices'];
        $json->total_staffs= $attributes['total_staffs'];
        $json1 = json_encode($json);

        $status = $attributes['status'];
        $total_price = str_replace(',','', $attributes['total_price']);

        DB::table('orders')
            ->where('id', $id)
            ->update(['data' => $json1,'status' => $status, 'price' => $total_price]);

    }

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

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