【问题标题】:How to sync pivot with extra field in Laravel?如何在 Laravel 中将枢轴与额外字段同步?
【发布时间】: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,如果我请你帮忙的话,我太打扰了?

标签: php laravel


【解决方案1】:

我建议您查看有关表单的 laravel 教程并重新考虑这种方法,即 select 可能不是执行此操作的最佳方法。选择不是解决这个问题的最佳方法。

尝试这样做:

@foreach($pellet->specie as $specie)
     {!! Form::open(["url" => "composizione/pellet/update/".$pellet->id, "method" => "PUT"]) !!}
     <tr>
        <td>
            <input type="hidden" name="specie" value={{$specie->id}} />
            <input type="text" class="form-control" name="percentuale" value="{{ $specie->pivot->percentuale }}">
        </td>
        <td><inpyt type="submit" class="btn btn-danger">Elimina</a> </td>
    </tr>
   {!! Form::close() !}}
@endforeach

其余的可能会起作用。这里发生的情况是您将一次更新单个 specie / percentuale 组合,而不是多个组合。

【讨论】:

  • 感谢 apokryfos 现在可以工作,但对我来说不是正确的方法,因为我需要同时更新颗粒的所有成分。当用户填写表格时,必须更新所有的树。
  • 我知道问题是同步。如果我手动设置: $pellet->specie()->sync([1 => ['percentuale' => 50], 7 => ['percentuale' => 50]]);它工作正常。
  • 同步所以更新 id 为 5 且物种 id 为 1 的颗粒至百分比 50,以及 id 为 5 且物种 id 为 7 至 50 的颗粒
  • 我发现了一个错误。我已经用 array_map 解决了……谢谢。
猜你喜欢
  • 1970-01-01
  • 2016-01-23
  • 2017-11-18
  • 2015-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-30
  • 1970-01-01
相关资源
最近更新 更多