【问题标题】:Laravel : Form Data BindingLaravel:表单数据绑定
【发布时间】:2016-01-04 08:26:16
【问题描述】:

我需要使用表单数据绑定将数据绑定到相关字段,但数据根本没有显示在字段上。我不知道究竟是什么问题阻止了数据出现。

控制器:

public function edit($id){
$datum = RatingDatum::findorfail(1);
 return view('rating.index',compact('datum'));
}

查看:

<div class="plan bg-plan">
   {!! Form::model($datums,['method' => 'PATCH','action'=>'RatingDataController@update'],$datums->id]) !!}  
   <div class="radio">
           <label>
             {!! Form::radio('customize_question_id')!!}{{$datums->value}}
            </label>
   </div>

   <div class="form-group">
       {!! Form::label('comment','Comment :') !!}
       {!! Form::textarea('comment' ,null,['class'=>'form-control', 'rows' => 4]) !!}

       {!! Form::label('reference','Reference:') !!}
       {!! Form::textarea('reference',null,['class'=>'form-control', 'rows' => 1]) !!}
   </div>
       {!! Form::submit('Submit Data', ['class' => 'btn btn-success submit']) !!}
       {!! Form::close() !!}
   </div>

dd($datum);

#attributes: array:15 [▼
    "id" => 1
    "organisation_id" => 8
    "sector_id" => 1
    "country_id" => 1
    "dimension_id" => 12
    "question_angle_id" => 1
    "customize_criteria_id" => 33
    "customize_question_id" => 7591
    "question_weight" => 20
    "actual_score" => 75
    "value" => "The company has made a formal commitment to promoting voluntary community initiatives and has set up quantitative targets in this regard."
    "comment" => ""
    "reference" => ""
    "created_at" => "2015-12-21 11:28:38"
    "updated_at" => "2015-12-21 12:22:25"
  ]

【问题讨论】:

  • 如您所见,referencecomment 为空,因此文本框将为空
  • 是数据绑定,即使字段为空,数据也会自动添加到相关字段中
  • 我的意思是说数据库中的值是空的。检查您的dd 数据
  • 单选按钮也有问题:(
  • {!! Form::radio('customize_question_id', $datums-&gt;customize_question_id, $datums-&gt;customize_question_id != null ? 'true' : 'false')!!}

标签: php forms data-binding laravel-5


【解决方案1】:

数据未显示在表单中是因为您引用了错误的变量。

您正在传递$datum,并且在视图表单中,您正在访问$datums。这就是您的表单为空的原因。

解决方案:

执行以下任一操作:

  • 将控制器方法的$datum 更新为$datums

  • 将表单模型$datums 更新为$datum

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2013-05-28
    • 2018-09-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多