【发布时间】:2017-07-16 22:27:48
【问题描述】:
我需要一些有关 Laravel 问题的帮助。我有 3 张桌子:
- 颗粒
- 物种(有树)
- specie_pellet(这是数据透视表)
specie_pellet 表是颗粒组成 的轴心,并有一个称为percentuale(百分比)的额外字段。 显然关系是 Specie 类中的 belongsToMany 和 Pellet 类中的 belongsToMany 详细Pellet Class:
// Imposto la relazione con le Specie
public function specie()
{
return $this->belongsToMany(Specie::class)->withPivot('percentuale');
}
详细物种分类:
// Imposto la relazione con i Pellets
public function pellets()
{
return $this->belongsToMany(Pellet::class)->withPivot('percentuale');
}
要修改表格,我使用表格:
{!! Form::model($pellet, ['method' => 'PUT', 'route' => ['composizione.update', $pellet->id]]) !!}
<table class="table table-bordered">
<thead>
<th>Specie</th>
<th>Percentuale</th>
<th>Azione</th>
</thead>
<tbody>
@foreach($pellet->specie as $specie)
<tr>
<td>{!! Form::select('specie[]', $listaSpecie, $specie->id, ['class' => 'form-control']) !!}</td>
<td>
<input type="text" class="form-control" name="percentuale[]" value="{{ $specie->pivot->percentuale }}">
</td>
<td><a class="btn btn-danger" href="{{ url('composizione/pellet/elimina/id/' . $pellet->id . '/specie/' . $specie->id) }}">Elimina</a> </td>
</tr>
@endforeach
</tbody>
</table>
{!! Form::submit('Aggiorna Composizione', ['class' => 'btn btn-success']) !!}
{!! Form::close() !!}
请求传递给路由
Route::put('composizione/pellet/update/{id}', 'PelletController@updateComposizione')->name('composizione.update');
最后我有了控制器
public function updateComposizione(\App\Http\Requests\ComposizioneUpdateRequest $request, $id)
{
$pellet = Pellet::find($id);
$specie = Input::get('specie');
$percentuali = Input::get('percentuale');
$pellet->specie()->sync([$specie => ['percentuale' => $percentuali]]);
return redirect('admin/pellets/')->with('ok_success', 'Composizione aggiornata correttamente');
}
但我收到此错误:非法偏移类型
in PelletController.php line 94
at HandleExceptions->handleError(2, 'Illegal offset type', '/srv/users/serverpilot/apps/laraweb/app/Http/Controllers/PelletController.php', 94, array('request' => object(ComposizioneUpdateRequest), 'id' => '5', 'pellet' => object(Pellet), 'specie' => array('1', '7'), 'percentuali' => array('50', '50'))) in PelletController.php line 94
at PelletController->updateComposizione(object(ComposizioneUpdateRequest), '5')
at call_user_func_array(array(object(PelletController), 'updateComposizione'), array(object(ComposizioneUpdateRequest), 'id' => '5')) in Controller.php line 55
...
有人可以帮我理解这个吗? 非常感谢,对不起我的英语。我是意大利人;)
【问题讨论】:
-
Form::select('specie[]'表示您的选择将在请求中放入一个数组。你应该做Form::select('specie' -
感谢您的回复。我有 specie[] 因为我必须传递多个值。例如,Id 5 的颗粒可以有 Id 1 的种类和 50% 的百分比,但也可以有 Id 7 的种类和 50% 的百分比
-
你需要重新考虑你的表单。
-
抱歉apokryfos,如果我请你帮忙的话,我太打扰了?