繁体   English   中英

Laravel'Form :: 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() !!}

问题在于,在“种类/ {id} /编辑”路径中,shore_weight输入根本没有填充任何值。 我在Species模型中设置了变量器和访问器,因为我将此值存储为数据库中的整数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();
        });

Helper函数可将权重转换为Dram和From Dram:

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

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

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

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

暂无
暂无

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

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