【问题标题】:Laravel 'Form::model()' does not populate input values stored as an arrayLaravel 'Form::model()' 不会填充存储为数组的输入值
【发布时间】:2016-04-08 22:39:26
【问题描述】:

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

{!! 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 路线中时,shore_weight 输入根本没有填充值。我在我的 Species 模型中设置了一个 mutator 和 accessor,因为我将此值作为整数 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];
}

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

【问题讨论】:

    标签: php forms laravel eloquent blade


    【解决方案1】:

    事实证明,这个错误完全是我自己造成的!我所要做的就是将输入的默认值设置为 null,如下所示:

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

    这就解决了问题!希望有人觉得它有用!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-22
      • 2020-05-06
      • 2015-11-03
      • 2019-03-03
      • 2015-04-15
      • 2015-03-30
      • 1970-01-01
      相关资源
      最近更新 更多