簡體   English   中英

如何根據laravel復選框值更新數據庫

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

我從數據庫中加載復選框,它們是否被選中,

<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>

現在我想根據復選框值更新我的數據庫。

例如,如果說我從數據庫中加載復選框 1,但現在未選中,我需要在數據庫中更新它。

這段代碼我可以獲得復選框的所有當前狀態,但我不知道它以前的值。 所以它很難更新當前值的狀態並在數據庫中添加新狀態,

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

這是我保存在數據列中的 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"}

我加載 [order_info][products][type] 作為檢查的產品,同時我從 env 文件加載所有產品。 我只需要在數據庫中保存選中的復選框產品。

有人可以幫助我嗎?

這是我為我的問題所做的完整工作解決方案!

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