我有一个这样的表格(这里我只介绍一些输入):
{!! Form::model($species, [
'method' => 'PUT',
'route' => ['app.species.update', $species->id],
'id' => 'update-species']) !!}
{!! Form::text('name', null, [
'class' => 'form-control',
'id' => 'name',
'placeholder' => 'John Dory']) !!}
{!! Form::select('type_id', $speciesTypes, 0, [
'class' => 'form-control',
'id' => 'type-id']) !!}
{!! Form::text("shore_weight[lbs]", 0, [
'class' => 'form-control',
'id' => 'shore-weight-lbs']) !!}
{!! Form::text('shore_weight[oz]', 0, [
'class' => 'form-control',
'id' => 'shore-weight-oz']) !!}
{!! Form::text('shore_weight[dr]', 0, [
'class' => 'form-control',
'id' => 'shore-weight-dr']) !!}
{!! Form::close() !!}
问题是,在物种/ {id} /编辑路线中,shore_weight输入根本没有填充值。我在我的Species模型中设置了一个mutator和accessor,因为我将此值存储为数据库中的整数dr(drams)。但需要允许用户输入重量以磅盎司为单位的重量。访问器和mutator看起来像这样:
/**
* Convert shore_weight to drams
*
* @param $value
*/
public function setShoreWeightAttribute($value)
{
$this->attributes['shore_weight'] = toDrams($value);
}
/**
* Convert shore_weight to 'lbs oz dr' format and return as an array
*
* @param $value
* @return array
*/
public function getShoreWeightAttribute($value)
{
return fromDrams($value);
}
Schema::create('species', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('type_id')->unsigned();
$table->foreign('type_id')
->references('id')
->on('species_types');
// Store weight in Drams
// http://gwydir.demon.co.uk/jo/units/weight.htm
$table->integer('shore_weight')->unsigned();
$table->integer('boat_weight')->unsigned();
$table->integer('wreck_weight')->unsigned();
$table->timestamps();
});
Helper用于将权重转换为Drams和Drams:
/**
* Helper function to convert weight in pounds,
* ounces, drams format to Drams only. Accepts
* an array or each value separately.
*
* @param int $lbs Pounds
* @param int $oz Ounces
* @param int $dr Drams
* @return int
*/
function toDrams($lbs = 0, $oz = 0, $dr = 0){
if(func_num_args() == 1 && is_array($lbs))
{
return (($lbs['lbs'] * 16 * 16) + ($lbs['oz'] * 16) + $lbs['dr']);
}
// Returns integer
return (int) (($lbs * 256) + ($oz * 16) + $dr);
}
/**
* Converts weight in drams back to pounds, ounces, drams
* format.
*
* @param $drams
* @return array
*/
function fromDrams($drams){
// Nullify pounds
$ouncesAndDrams = $drams % 256;
$lbs = (int) ($drams / 256);
$oz = (int) ($ouncesAndDrams / 16);
$dr = $ouncesAndDrams % 16;
// Returns an array
return ['lbs' => $lbs, 'oz' => $oz, 'dr' => $dr];
}
$ species->由于访问者,shore_weight属于数组类型。一个例子是:
/**
* If in database shore_weight equals to 273
* output in the view when retrieving it with the
* model would be:
*/
$species->shore_weight = ['lbs' => 1, 'oz' => 1, 'rd' => 1];
我可以考虑使用一些变通方法,但是我真的想使用本机模型绑定...
有什么方法可以让它发挥作用吗?我甚至尝试将数组转换为一个对象,并在FormBuilder类中查看了究竟发生了什么,但到目前为止还没有运气。
我将不胜感激任何帮助。 非常感谢
在找到更好的方法之前,我正在使用一种肮脏的解决方法,我手动设置默认值:
{!! Form::text("shore_weight[lbs]", (@$species->shore_weight['lbs']) ?: 0, [
'class' => 'form-control',
'id' => 'shore-weight-lbs']) !!}
答案 0 :(得分:0)
事实证明,错误完全取决于我!我所要做的就是将输入的默认值设置为null,如下所示:
{!! Form::text("shore_weight[lbs]", null, [
'class' => 'form-control',
'id' => 'shore-weight-lbs']) !!}
并解决了这个问题!希望有人发现它有用!