簡體   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