0
many to many 관계 데이터로 입력을 바인딩하는 방법은 무엇입니까?many to many 양식 바인딩
내 관계는 다음과 같습니다. 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"가 아니라 "Human Borges"와 같아. : P 당신은 다른 이름 - 그것의 임무를 반영하는 이름을 주어야한다고 생각해야합니다. – chris342423
모델 이름은 동일한 모델이며, 예를 들어 자동차 모델입니다. –