亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

PHP
海綿寶寶撒 2022-05-27 13:16:22
我從數據庫加載復選框,它們是否被選中,<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 對象我加載 [order_info][products][type] 作為檢查的產品,同時我從 env 文件加載所有產品。我只需要將選中的復選框產品保存在數據庫中。有人可以幫助我嗎?
查看完整描述

1 回答

?
胡子哥哥

TA貢獻1825條經驗 獲得超6個贊

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


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]);


    }



查看完整回答
反對 回復 2022-05-27
  • 1 回答
  • 0 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號