Laravel';窗体::model()';不填充存储为数组的输入值


Laravel 'Form::model()' does not populate input values stored as an array

我有一个这样的表单(这里我只显示一些输入):

{!! 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() !!}

问题是,在species/{id}/edit路由中,shoreweight输入根本没有填充值。我在我的物种模型中设置了一个突变器和访问器,因为我将这个值作为整数dr(drams)存储在数据库中。但需要允许用户输入磅、盎司和德拉姆的重量。存取器和变异器如下所示:

物种模型:

/**
 * 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();
        });

将重量转换为Drams和From 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];
}

$special->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']) !!}

原来错误完全是我造成的!我所要做的就是将输入的默认值设置为null,如下所示:

{!! Form::text("shore_weight[lbs]", null, [
    'class' => 'form-control', 
    'id' => 'shore-weight-lbs']) !!}

这就解决了问题!希望有人发现它有用!