为什么我的变量在Laravel 5.4 Blade文件中显示的值不正确?

alt*_*ids 3 php laravel blade laravel-5

我有一个变量$country_code,它在我的表单的一个部分显示正确的值,但不是在不同的部分.为什么会这样?

这是我的代码:

{{ Form::open(['action' => ['PinVerificationController@valid'],'id'=>'pin_code_form']) }}
    //$country_code shows 1
    We sent a text message to {{$country_code}} {{$phone_number}}. You should receive it within a few seconds.<br><br>
    {{ Form::label('Pin Code', null, ['class' => 'control-label']) }}
    {{ Form::hidden('country_code', $country_code) }}//<------shows 1-US instead of 1
    {{ Form::hidden('phone_number', $phone_number) }}
    {{ Form::hidden('type', $pin_notification_type) }}
    {{ Form::text('pin_code', null,['placeholder' => 'Pin Code'])}}<br><br>
    Enter a 4 digit pin you received by phone.
    <br>
    <br>
    {{ Form::submit('Verify',['name'=>'validate'])}}

{{ Form::close() }}
Run Code Online (Sandbox Code Playgroud)

因此,如果我在控制器中将$ country_code设置为"1",它将显示我们发送了一条短信给1 5555555.你应该在几秒钟内收到它. 但是,如果我在隐藏的表单上执行检查元素,则会显示1-US.我试过了php artisan view:clear,php artisan clear-compiled但问题仍然存在.

我也试过硬编码值{{ Form::hidden('country_code', 'asdf') }},我没有看到变化.我尝试添加测试{{ Form::hidden('country_code1', 'asdf') }}并查看更新.

我也改名country_codecountry_code111我的隐藏字段,它显示的1正确的价值我认为这是一个缓存的问题,但就像我提到我已经试过php artisan cache:clear,问题依然存在.

Bra*_*rad 6

由于您使用的是Laravel 5.4,我假设您使用Form的是LaravelCollective,因为它们是从5.x中的基线Laravel中删除的.

如果LaravelCollective Forms存在于请求数据中或旧发布的数据(old()函数)中,它将覆盖您提供给输入的值.我怀疑你是这样的.

您可以在此处查看此行为实现.

要解决此问题,您有以下几种选择:

  1. 更改送入页面的请求参数的名称(如果您可以控制它)
  2. 将您的字段名称重命名为不冲突的内容
  3. 不要Form::用来生成表单,只需使用经典的html/Blade自动创建隐藏的输入

就个人而言,我建议#3,因为那样你就可以完全控制你的代码了.

<input type="hidden" name="country_code" value="{{ $country_code }}"/>
Run Code Online (Sandbox Code Playgroud)