Laravel Blade向控制器返回空值

时间:2019-03-17 11:10:12

标签: laravel laravel-5 laravel-5.2

*问题已回答*

我试图让表单文本字段从刀片输入值到控制器变量,然后传递给数据库查询,但它为我提供了空值。

例如,  $ a = request('chargeable_price');

这给了我空值。有人能帮我吗? 代码运行无错误,但数据库字段更新为零而不传递值。

这是我的控制器代码

public function postEditSubscription(Request $request, $property_id, $subscription_id){


        try {

            $a = request('chargeable_price');

            DB::table('subscriptions')->where('id',$subscription_id)->update(array(
                'price'=>$a,
            ));


        }catch(ModelNotFoundException $e){

            throw $e;

        }catch(ModelValidationException $e){


            throw $e;

        }catch(IntegrityException $e){

            throw $e;

        }catch(Exception $e){


            throw $e;

        }

      //  $request->flush();

        return redirect()->route('admin::managing::subscription::index', array('property_id' =>$property_id))->with(Sess::getKey('success'), Translator::transSmart('app.You have successfully edit Subscription Details.', 'You have successfully edit Subscription Details.'));


    }

这是我的刀片服务器代码

@section('content')

    <div class="admin-managing-subscription-invoice">

        <div class="row">

            <div class="col-sm-12">
                <div class="page-header">
                    <h3>
                        {{Translator::transSmart('app.EditSubscription', 'Edit Subscription')}}
                    </h3>
                </div>
            </div>
        </div>




        <div class="row">
            <div class="col-sm-12">

                {{ Form::open(array('class' => 'form-horizontal booking-form')) }}

                <form class="form-horizontal booking-form" method="post" action="{{ route('admin::managing::subscription::post-edit-subscription', ['property_id' => $subscription->property_id, 'subscription_id' => $subscription->id]) }}">
                    {{ csrf_field() }}


                    @php
                    $reservedPeriodDetails = array(
                        'reserved_days' => $subscription->getReservedDays(),
                        'days_in_calendar_month_by_current_reserved_period' => $subscription->getDaysInCalendarMonthByCurrentReservedPeriod()
                    );
                @endphp
                <div class="hide reserved-period" data-details="{{json_encode($reservedPeriodDetails)}}"></div>
                <div class="row">
                    <div class="form-group">
                        <?php
                        $field = 'start_date';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);
                        $translate = Translator::transSmart('app.Subscription Date', 'Subscription Date');
                        $start_date = old('start_date', $subscription_table->start_date);
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-4 input-group schedule">
                            {{Form::text($name, $subscription->getAttribute($field) , array('id' => $name, 'class' => 'form-control datepicker start_date', 'readonly' => 'readonly', 'title' => 'Subscription date', 'placeholder' => ''))}}
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <?php
                        $field2 = 'end_date';
                        $name2 = sprintf('%s[%s]', $subscription->getTable(), $field2);
                        $translate2 = Translator::transSmart('app.Subscription End Date', 'Subscription End Date');
                        ?>
                        <label for="{{$name2}}" class="col-sm-2 control-label">{{$translate2}}</label>
                        <div class="col-sm-4 input-group schedule">
                            {{Form::text($name2, $subscription->getAttribute($field2) , array('id' => $name2, 'class' => sprintf('%s form-control datepicker', $field2),  'readonly' => 'readonly',  'title' => 'Subscription date', 'placeholder' => ''))}}
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>

                    <div class="form-group">
                        <?php
                        $field = 'name';
                        $name = sprintf('%s[%s]', $package->getTable(), $field);
                        $translate = Translator::transSmart('app.Package Name', 'Package Name');
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation($package, $field)}}
                            <p class="form-control-static">{{$package->getAttribute($field)}}</p>
                        </div>
                    </div>
                    <div class="form-group">
                        <?php
                        $field = 'category_name';
                        $name = sprintf('%s[%s]', $package->getTable(), $field);
                        $translate = Translator::transSmart('app.Package Category', 'Package Category');
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation($package, $field)}}
                            <p class="form-control-static">{{$package->getAttribute($field)}}</p>
                        </div>
                    </div>

                    <div class="form-group required">
                        <?php
                        $field = 'contract_month';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);
                        $translate = Translator::transSmart('app.Contract Month', 'Contract Month');
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation($subscription, $field)}}
                            @php
                                $contract_months = [];
                                for($i = 1; $i <= 24; $i++){
                                    $contract_months[$i] = $i;
                                }
                                $contract_month = (old('contract_month', $subscription_table->contract_month)) - 1;
                                $default_month = $contract_month;
                                if(\Request::get('subscriptions') !== null && \Request::get('subscriptions')['contract_month'] > 0){
                            $default_month = \Request::get('subscriptions')['contract_month'];
                        }
                            @endphp
                            {{Form::select($name, $contract_months, $default_month, array('id' => $name, 'class' => sprintf('%s form-control', $field), 'title' => $translate))}}
                        </div>
                    </div>

                    <div class="form-group">
                        <?php
                        $field = 'price';
                        $name = sprintf('%s[][price]', $subscription_month_price->getTable());
                        $translate = Translator::transSmart('app.Regular Price', 'Regular Price');
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">
                            <div>
                                {{$translate}}
                            </div>
                            <div>
                                {{Translator::transSmart('app.(Per Seat)', '(Per Seat)')}}
                            </div>
                        </label>
                        <div class="col-sm-10 prices">
                            @php
                                $contract_month = (old('contract_month', $subscription_table->contract_month) - 1);
                                    $default_month = $contract_month;
                                    if($subscription->contract_month > 0){
                                        $default_month = $subscription->contract_month;
                                    }
                            @endphp

                            @for ($i = 0; $i < $default_month; $i++)

                                @php
                                    $name = sprintf('%s[%d][price]', $subscription_month_price->getTable(), $i);
                                   // $default_price = old('subscription_month_price.'.$i.'.price', $subscription->getAttribute($field));
                                    $default_price = old('.price', $subscription_month_price_table->price.$i);

                                     // $deposit = old('deposit', $subscription_table->deposit);

                                 //   if($i == 0){
                                  //      $default_price = old('subscription_month_price.'.$i.'.price', $subscription->getAttribute('pro_rate_price'));
                                   // }
                                @endphp
                                <div class='month-price-row'>
                                    <p class="form-control-static">Month: {{$i + 1}}</p>
                                    <div class="input-group">
                                        <span class="input-group-addon">{{$property->currency}}</span>
                                        {{Form::text($name, CLDR::number($default_price, Config::get('money.precision')), array('id' => $name, 'class' => sprintf('%s form-control price-value subscription-month-price', $field), 'title' => $translate, 'data-normal-price'=>$subscription->getAttribute($field)))}}
                                    </div>
                                </div>

                            @endfor


                        </div>
                    </div>

                    <div class="form-group required">
                        <?php
                        $field = 'seat';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);
                        $translate = Translator::transSmart('app.Seat', 'Seat');
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation($subscription, $field)}}
                            {{Form::text($name,  $subscription->getAttribute($field) , array('id' => $name, 'class' => sprintf('%s form-control integer-value', $field), 'title' => $translate, 'placeholder' => $translate, 'readonly'=>'readonly'))}}
                        </div>
                    </div>

                    <div class="form-group required">
                        <?php
                        $field = 'deposit';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);
                        $translate = Translator::transSmart('app.Deposit', 'Deposit');
                        $translate1 = Translator::transSmart('app.Only allow this format "#.##".', 'Only allow this format "#.##".');
                        $deposit = old('deposit', $subscription_table->deposit);
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation($subscription, $field)}}
                            {{Form::text($name, $deposit , array('id' => $name, 'class' => sprintf('%s form-control price-value', $field), 'title' => $translate, 'placeholder' => $translate1))}}
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-12">
                            <hr />
                            <h3>
                                {{Translator::transSmart('app.Package (First Month)', 'Package (First Month)')}}
                            </h3>
                            <hr />
                        </div>
                    </div>

                    <div class="form-group">
                        <?php
                        $field = 'chargeable_price';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);
                        $translate = Translator::transSmart('app.Package', 'Package');
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">
                            {{$translate}}
                            <a href="javascript:void(0);" class='help-box' data-container="body" data-toggle="popover" data-trigger="focus"  data-placement="top" data-html="true" data-content="{{Translator::transSmart('app.Package is inclusive of tax if any.', 'Package is inclusive of tax if any.')}}">
                                <i class="fa fa-question-circle fa-lg"></i>
                            </a>
                        </label>
                        <div class="col-sm-10">
                            {{Html::validation($subscription, $field)}}
                            <div class="input-group">
                                <span class="input-group-addon">{{$property->currency}}</span>
                                {{Form::text($name, CLDR::number($subscription->chargeablePrice(), Config::get('money.precision')), array('id' => $name, 'readonly' => 'readonly', 'class' => sprintf('%s form-control price-value', $field), 'title' => $translate))}}
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <?php
                        $field = 'tax';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);print_r($property->is_taxable);
                        $tax_value = (($property->is_taxable == true)?$property->tax_value:0);
                        $translate = Translator::transSmart('app.Tax (%s)', sprintf('Tax (%s)', CLDR::showTax($tax_value)), true, ['tax' => CLDR::showTax($tax_value)])
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation($subscription, $field)}}
                            <div class="input-group">
                                <span class="input-group-addon">{{$property->currency}}</span>
                                {{Form::text($name, CLDR::number($subscription->tax(), Config::get('money.precision')), array('id' => $name, 'readonly' => 'readonly', 'class' => sprintf('%s form-control price-value', $field), 'data-is-taxable' => $subscription->is_taxable, 'data-tax-value' => $property->tax_value,'title' => $translate))}}
                            </div>
                        </div>
                    </div>
                    <div class="form-group">
                        <?php
                        $field = 'deposit';
                        $field1 = 'gross_deposit';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field1);
                        $translate = Translator::transSmart('app.Deposit', 'Deposit');
                        $deposit = old('deposit', $subscription_table->deposit);
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">
                            {{$translate}}
                            <a href="javascript:void(0);" class='help-box' data-container="body" data-toggle="popover" data-trigger="focus"  data-placement="top" data-html="true" data-content="{{Translator::transSmart('app.Deposit will be applied if amount is more than zero.', 'Deposit will be applied if amount is more than zero.')}}">
                                <i class="fa fa-question-circle fa-lg"></i>
                            </a>
                        </label>
                        <div class="col-sm-10">
                            {{Html::validation($subscription, $field1)}}
                            <div class="input-group">
                                <span class="input-group-addon">{{$property->currency}}</span>
                                {{Form::text($name, $deposit, array('id' => $name, 'readonly' => 'readonly', 'class' => sprintf('%s form-control price-value', $field1), 'title' => $translate))}}
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <?php
                        $field = 'gross_price_and_deposit';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);
                        $translate = Translator::transSmart('app.Gross Total', 'Gross Total');
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">
                            {{$translate}}
                            <a href="javascript:void(0);" class='help-box' data-container="body" data-toggle="popover" data-trigger="focus"  data-placement="top" data-html="true" data-content="{{Translator::transSmart('app.Gross total is inclusive of deposit if any.', 'Gross total is inclusive of deposit if any.')}}">
                                <i class="fa fa-question-circle fa-lg"></i>
                            </a>
                        </label>
                        <div class="col-sm-10">
                            {{Html::validation($subscription, $field)}}
                            <div class="input-group">
                                <span class="input-group-addon">{{$property->currency}}</span>
                                {{Form::text($name, CLDR::number($subscription->grossPriceAndDeposit($subscription->seat), Config::get('money.precision')), array('id' => $name, 'readonly' => 'readonly', 'class' => sprintf('%s form-control price-value', $field), 'title' => $translate))}}
                            </div>
                        </div>
                    </div>

                    <div class="form-group required">
                        <?php
                        $field = 'user_id';
                        $name = sprintf('%s[%s]',  $subscription_user->getTable(), $field);
                        $name1 = sprintf('%s[%s]', 'typeahead', $field);
                        $translate = Translator::transSmart('app.Member', 'Member');
                        $member_name = old('full_name', $users_table->full_name);
                        $route = URL::route('admin::managing::member::subscription-package', array('property_id' => $property->getKey(), 'package_id' => $package->getKey()));

                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation( $subscription_user, $field)}}
                            {{Form::hidden($name, $subscription_user->getAttribute($field), array('class' => sprintf('%s_hidden form-control', $field)))}}
                            <div class="twitter-typeahead-container">
                                {{Form::text($name1, $member_name, array('id' => $name1, 'class' => sprintf('%s form-control', $field), 'data-url' => $route, 'data-no-found' => Translator::transSmart('app.No Found.', 'No Found'),  'autocomplete' => 'off',  'title' => $translate, 'placeholder' => Translator::transSmart('app.Search by name, username or email.', 'Search by name, username or email.')))}}
                            </div>
                        </div>
                    </div>
                    <div class="form-group required">
                        <?php
                        $field = 'company_id';
                        $name = sprintf('%s[%s]', $subscription->getTable(), $field);
                        $translate = Translator::transSmart('app.Company', 'Company');
                        $company_Name = old('name', $company->name);
                        ?>
                        <label for="{{$name}}" class="col-sm-2 control-label">{{$translate}}</label>
                        <div class="col-sm-10">
                            {{Html::validation( $subscription, $field)}}
                            {{Form::hidden($name, '', array('class' => 'company-hidden'))}}
                            <a href="javascript:void(0);" class="add-company" data-url="{{URL::route('admin::member::add-company')}}" style="float:right;margin-bottom: 5px;">
                                {{Translator::transSmart('app.Add Company', 'Add Company')}}
                            </a>
                            <div style="clear:both;"></div>
                            <div class="twitter-typeahead-container">
                                {{Form::text('company', $company_Name, array('id' => 'company', 'class' => sprintf('%s form-control', 'company'), 'data-url' => URL::route('api::company::search'), 'data-member-rights' => true, 'data-edit-url' => URL::route('admin::member::edit-company', array('id' => '')), 'data-edit-word' => Translator::transSmart('app.Edit', 'Edit'),'data-no-found' => Translator::transSmart('app.No Found.', 'No Found'),  'autocomplete' => 'off',  'title' => Translator::transSmart('app.Name', 'Name'), 'placeholder' => ''))}}
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-sm-offset-2 col-sm-10">

                            <div class="btn-group">


                                {{Form::submit(Translator::transSmart('app.Update', 'Update'), array('title' => Translator::transSmart('app.Update', 'Update'), 'class' => 'btn btn-theme btn-block submit payment-utility-pay')) }}



                            </div>
                            <div class="btn-group">
                                @if(isset($is_from_lead) && $is_from_lead)
                                    <a href="javascript:void(0);"
                                       title = "{{Translator::transSmart('app.Cancel', 'Cancel')}}"
                                       class="btn btn-theme btn-block cancel payment-utility-cancel" onclick = "widget.popup.close(false, null, 0)" >
                                        {{Translator::transSmart('app.Cancel', 'Cancel')}}
                                    </a>
                                @else
                                    {{Form::submit(Translator::transSmart('app.Cancel', 'Cancel'), array('title' => Translator::transSmart('app.Cancel', 'Cancel'), 'class' => 'btn btn-theme btn-block cancel payment-utility-cancel', 'onclick' => 'location.href="' . URL::getAdvancedLandingIntended('admin::managing::subscription::index', [$property->getKey(), $deposit],  URL::route('admin::managing::subscription::index', array('property_id' => $property->getKey()))) . '"; return false;')) }}
                                @endif
                            </div>
                        </div>
                    </div>





                </div>

                </form>


            </div>



        </div>
    </div>





    </div>

@endsection

stack

2 个答案:

答案 0 :(得分:0)

尝试访问类似$ a = $ request-> input('chargeable_price');的字段

答案 1 :(得分:0)

阅读屏幕截图,您的答案是

  • $request['subscriptions']['chargeable_price'];

  • request('subscriptions.chargeable_price')

完全由您决定使用哪一个。