简体   繁体   English

Laravel选择框,默认显示数据库值

[英]Laravel select box, display database value by default

I'm using a select box in laravel 我在laravel中使用选择框

<td>{!! Form::select('statCode', array('A' => 'Yes', 'D' => 'No')) !!}</td>

statCode is a column in the database table that this reads and writes to and I have it in the controller as $i->statCode statCode是数据库表中读取和写入的列,我在控制器中将其保存为$i->statCode

Is there a way I can modify this so that it shows the $i->statCode as the default value of the select box? 有没有一种方法可以修改它,以便将$i->statCode为选择框的默认值? Currently it just shows 'Yes' by default so if I save with the select box as 'No' it reflects the change in the database but if I go back in to edit it the box is back to 'Yes' as the default and I want it to always show the database value as the default. 当前,默认情况下它仅显示“是”,因此,如果我将选择框保存为“否”,则反映了数据库中的更改,但是如果我再次对其进行编辑,则该框将默认设置恢复为“是”,并且希望它始终将数据库值显示为默认值。

The database holds either 'A' or 'D' so if the database value is 'A' i want 'Yes' to show, if it's 'D' I want 'No' to show 数据库保留“ A”或“ D”,所以如果数据库值为“ A”,我希望显示“是”,如果是“ D”,我希望显示“否”

The 3d parameter is the default value, so you can use it like this: 3d参数是默认值,因此您可以像这样使用它:

{!! Form::select('statCode', array('A' => 'Yes', 'D' => 'No'), old('statCode', $i->statCode)) !!}

This will reflect even if you submit the form and there is an error, the previously selected state will be checked. 即使您提交表单并且出现错误,这也将反映出来,将检查先前选择的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM