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

我从数据库加载复选框,它们是否被选中,


<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 文件加载所有产品。我只需要将选中的复选框产品保存在数据库中。


有人可以帮助我吗?


海绵宝宝撒
浏览 119回答 1
1回答

胡子哥哥

这是我为我的问题所做的完整工作解决方案!public function edit($id, $attributes){&nbsp; &nbsp; &nbsp; &nbsp; $orders = (new Agos())->load($id);&nbsp; &nbsp; &nbsp; &nbsp; $json = $orders->data;&nbsp; &nbsp; &nbsp; &nbsp; $json = json_decode($json);&nbsp; &nbsp; &nbsp; &nbsp; $checkedit= $attributes['check_list'];&nbsp; &nbsp; &nbsp; &nbsp; if (is_array($checkedit) || is_object($checkedit))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($checkedit as $chked1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $exists = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($json->products as $key => $val)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // update if products exists&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($val->type == $chked1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $val->status = 'true';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $exists = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(array_key_exists('status', $val)) {}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else{ $val->status = 'false';}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($chked1 == 'FIN_REPORTING')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = 'Finance reporting & book keeping';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif ($chked1 == 'FIN_ADVISORY')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = 'Finance Advisory';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif ($chked1 == 'PAYROLL_HRDB')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = 'PAYROLL_HRDB';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elseif ($chked1 == 'HR_ELEAVE')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name =&nbsp; &nbsp;'HR E-Leave';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $name = 'HR E-Claim';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if products not exists add new products&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(!$exists) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newproduct = new \stdClass();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newproduct->type = $chked1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newproduct->name = $name;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newproduct->is_fixed_price = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newproduct->currency = "MYR";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $newproduct->status = "true";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array_push($json->products, $newproduct);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; //remove all products that have status = false&nbsp; &nbsp; &nbsp; &nbsp; foreach($json->products as $index => $product) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( $product->status == "false") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($json->products[$index]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $json->products = array_values($json->products);&nbsp; &nbsp; &nbsp; &nbsp; json_encode($json, JSON_PRETTY_PRINT);&nbsp; &nbsp; &nbsp; &nbsp; //remove status element from products&nbsp; &nbsp; &nbsp; &nbsp; foreach($json->products as $index => $product) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; unset($product->status);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $json->products = array_values($json->products);&nbsp; &nbsp; &nbsp; &nbsp; json_encode($json, JSON_PRETTY_PRINT);&nbsp; &nbsp; &nbsp; &nbsp; $json->google_drive_url= $attributes['data'];&nbsp; &nbsp; &nbsp; &nbsp; $json->remark= $attributes['remark'];&nbsp; &nbsp; &nbsp; &nbsp; $json->status= $attributes['status'];&nbsp; &nbsp; &nbsp; &nbsp; $json->total_invoices= $attributes['total_invoices'];&nbsp; &nbsp; &nbsp; &nbsp; $json->total_staffs= $attributes['total_staffs'];&nbsp; &nbsp; &nbsp; &nbsp; $json1 = json_encode($json);&nbsp; &nbsp; &nbsp; &nbsp; $status = $attributes['status'];&nbsp; &nbsp; &nbsp; &nbsp; $total_price = str_replace(',','', $attributes['total_price']);&nbsp; &nbsp; &nbsp; &nbsp; DB::table('orders')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->where('id', $id)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ->update(['data' => $json1,'status' => $status, 'price' => $total_price]);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP