Mam*_*asa 4 php forms checkbox laravel
我在表单中使用了一个复选框,由于某些原因我无法存储复选框状态的值(已选中或未选中).
我正在使用表单模型绑定.
我的表格是:
{!! Form::model($profile, ['method' => 'PATCH', 'action' => ['ProfilesController@update', $profile->id]]) !!}
<div class="form-group">
{!! Form::label('wifi', 'Wifi') !!}
{!! Form::checkbox('wifi','yes', $profile->wifi) !!}Wifi
</div>
{!! Form::close() !!}
Run Code Online (Sandbox Code Playgroud)
我的架构:
$table->boolean('wifi')->nullable();
Run Code Online (Sandbox Code Playgroud)
但我也尝试用整数
我无法弄清楚我做错了什么
你的这段代码
{!! Form::checkbox('wifi','yes', $profile->wifi) !!}Wifi
Run Code Online (Sandbox Code Playgroud)
正在产生这个
<input checked="checked" name="wifi" type="checkbox" value="yes">
Run Code Online (Sandbox Code Playgroud)
这意味着您yes要将值发送到服务器,但您的列数据类型不是varchar/text.您将其设置为布尔值.
更新你的代码,因为你正在使用form model binding所以不需要popluate它,laravel会为你做这个.
{!! Form::checkbox('wifi') !!} Wifi
Run Code Online (Sandbox Code Playgroud)
此外,包括您的wifi键fillable和casts数组.像这样
protected $fillable = [ ..., 'wifi' ];
protected $casts = [ 'wifi' => 'boolean' ];
Run Code Online (Sandbox Code Playgroud)
注意:您的架构代码
$table->boolean('wifi')->nullable;
Run Code Online (Sandbox Code Playgroud)
nullable不是财产,它是一种功能.所以也要更新它
$table->boolean('wifi')->nullable();
Run Code Online (Sandbox Code Playgroud)
在那之后引用您的数据库迁移
php artisan migrate:refresh
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6136 次 |
| 最近记录: |