0
如何绑定具有多对多关系数据的输入?多对多表单绑定
我的关系是:a Model
有很多Damages
,而Damage
有很多Models
。在数据透视表中存在一个price
字段。 我需要用price
数据填充输入。
{{ Form::input('number', "prices[{$model->id}][{$damage->id}]") }}
我的模型:
class Model extends \BaseModel {
public function damages()
{
return $this->belongsToMany('Damage', 'prices_damages', 'model_id', 'damage_id')
->withPivot('price')
->withTimestamps();
}
}
透视表
Schema::create('prices_damages', function(Blueprint $table)
{
$table->increments('id');
$table->integer('model_id')->unsigned();
$table->integer('damage_id')->unsigned();
$table->float('price')->nullable();
$table->timestamps();
});
控制器
/**
* Display a index dashboard page.
*
* @return \Illuminate\Http\Response
*/
public function getDamages()
{
$models = \Model::orderBy('order')->get();
$damages = \Damage::orderBy('order')->get();
return $this->render('Avarias', 'prices.damages', compact('models', 'damages'));
}
查看:
<table class="table-striped table-header-rotated">
<thead>
<tr>
<th></th>
@foreach ($damages as $damage)
<th class="vertical"><div><span>{{ $damage->name }}</span></div></th>
@endforeach
</tr>
</thead>
<tbody>
@foreach ($models as $model)
<tr>
<td>{{ $model->fullname }}</td>
@foreach ($damages as $damage)
<td>
{{ Form::input('number', "prices[{$model->id}][{$damage->id}]", null, ['min' => 0, 'step' => 0.01]) }}
</td>
@endforeach
</tr>
@endforeach
</tbody>
</table>
“模型”?这就像你的名字不是“Miguel Borges”,而是“人类博尔赫斯”。 :P你应该考虑给它另一个名字 - 一个反映它的任务的名字。 – chris342423 2014-09-28 11:14:25
车型名称与车型相同,例如车型。 – 2014-09-28 15:20:48