使用Laravel/Eloquent填充数据透视表

使用Laravel/Eloquent填充数据透视表,laravel,eloquent,backpack-for-laravel,Laravel,Eloquent,Backpack For Laravel,我有8个表格:产品、害虫、活性物质、作物、活性物质、有害物质、作物和有害物质 我构建了一个表单,用于加载所选(农用化学品)产品的信息——在该表单中,用户选择与该产品相关的害虫、活性物质和作物。提交时,我的现有代码会将预期信息保存在products表中,并且通过一组“belongTomany”关系,活动的\u产品、pest\u产品和crop\u产品透视表也会正确更新 我的问题是,我不知道如何使用活动和有害生物信息(即它们各自的id值)来添加/更新活动的有害生物表 我想知道一些方向 我的模型中的方法

我有8个表格:产品、害虫、活性物质、作物、活性物质、有害物质、作物和有害物质

我构建了一个表单,用于加载所选(农用化学品)产品的信息——在该表单中,用户选择与该产品相关的害虫、活性物质和作物。提交时,我的现有代码会将预期信息保存在products表中,并且通过一组“belongTomany”关系,活动的\u产品、pest\u产品和crop\u产品透视表也会正确更新

我的问题是,我不知道如何使用活动和有害生物信息(即它们各自的id值)来添加/更新活动的有害生物表

我想知道一些方向

我的模型中的方法如下所示:

产品

public function Actives()
{
  return $this->hasMany('App\Models\Active','active_product', 'product_id', 'active_id');
}
public function pest()
{
  return $this->belongsToMany('App\Models\Pest','pest_product', 'product_id', 'pest_id');
}
public function active()
{
  return $this->belongsToMany('App\Models\Active','active_product', 'product_id', 'active_id');
}
活动的

public function product()
{
  return $this->belongsToMany('App\Models\Product', 'active_product', 'active_id', 'product_id');
}

public function pest()
{
  return $this->belongsToMany('App\Models\Pest', 'active_pest', 'active_id', 'pest_id');
}
害虫

public function active()
{
  return $this->belongsToMany('App\Models\Active', 'active_pest', 'pest_id', 'active_id');
}
public function product()
{
  return $this->belongsToMany('App\Models\Product','pest_product', 'pest_id', 'product_id');
}
public function crop()
{
  return $this->belongsToMany('App\Models\Crop','crop_pest', 'pest_id', 'crop_id');
}
我正在使用Laravel背包-我的产品控制器包含此更新功能:

public function update(UpdateRequest $request)
{    
    $redirect_location = parent::updateCrud($request);
    return $redirect_location;
}
updateCrud是

public function updateCrud(UpdateRequest $request = null)
{
    $this->crud->hasAccessOrFail('update');
    $this->crud->setOperation('update');

    // fallback to global request instance
    if (is_null($request)) {
        $request = \Request::instance();
    }

    // update the row in the db
    $item = $this->crud->update($request->get($this->crud->model->getKeyName()),
                        $request->except('save_action', '_token', '_method', 'current_tab', 'http_referrer'));
    $this->data['entry'] = $this->crud->entry = $item;

    // show a success message
    \Alert::success(trans('backpack::crud.update_success'))->flash();

    // save the redirect choice for next time
    $this->setSaveAction();

    return $this->performSaveAction($item->getKey());
}
谢谢,汤姆。你可以用拉威尔的方法:

$actives = App\Active::create([
    'someColumn' => 'test',
    'anotherColumn' => 'test',
]);

$pests = App\Pest::create([
    'someColumn' => 'test',
    'anotherColumn' => 'test',
]);

$actives->pest()->attach($pests);
          ^^^-relation name in model

它的工作原理和其他的不一样吗?正如在命名约定中一样,似乎没有问题。你面临什么问题?您在
活动
车型上使用了哪些方法?您正在将数据保存到表和透视表中吗?我的活动模型包含公共函数pest(){return$this->belongtomany('App\Models\pest','Active_pest','Active_id','pest_id');},我的pest模型包含公共函数Active(){return$this->belongtomany('App\Models\Active','Active_pest','pest_id','Active_id');}编辑您的问题,并向我们展示相关代码。这里没有什么可供我们继续的-为什么您可以更新某些数据透视,但不能更新其他数据透视?具体问题是什么?感谢您的指导。在阅读了关于attach()、sync()和SyncWithoutDetaching()的内容后,我选择了后者,因为我需要一组独特的数据,这些数据不会随着每个新产品的处理而被删除。