【问题标题】:Laravel: Many to Many relationsLaravel:多对多关系
【发布时间】:2016-02-26 23:54:16
【问题描述】:

我有这样的设置。

文档模型

public function plates()
{
    return $this->belongsToMany('App\Models\Plate')->withTimestamps();
}

平板模型

public function documents()
{
    return $this->belongsToMany('App\Models\Document')->withTimestamps();
}

和这样的数据透视表

  public function up()
    {
        Schema::create('document_plate', function (Blueprint $table) {
          $table->integer('plate_id')->unsigned()->index();
          $table->integer('document_id')->unsigned()->index();

          $table->primary(['plate_id', 'document_id']);

          $table->timestamps();
        });
    }

现在我理解这种关系是多对多的,这意味着我可以将许多plate id 分配给许多document id,对吗?

这是我一直在玩的。

$plates = [
    'plate_id_1' => 1,
    'plate_id_2' => 2,
    'plate_id_3' => 3,

  ];


  $docs = [
    'recipe_id' => 1,
    'procedure_id' => 2,
  ];

  // attach many docs to plates
 $plate       =   Plate::findMany($plates);
 $doc         =   Document::findMany($docs);

 $plate->documents()->attach($doc->id);

我想要的是:当user 从下拉列表中选择documents 时,我想要将这些选定的文档分配给许多 选定的板块。所以它有很多带有很多盘子的文档。我已经尝试并检查了文档http://laravel.com/docs/5.1/eloquent-relationships#inserting-related-models

syncattach 方法。

上面的代码产生错误

调用未定义的方法 Illuminate\Database\Eloquent\Collection::documents()

任何想法我做错了什么?我这样做对吗?

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您正在尝试从不起作用的整个模型集合中检索模型的关系。我假设您的 find many 方法是根据您传递给它的 id 数组返回模型集合。

    如果您想在文档中附加许多图版,请使用:

    $document->plates()->attach($plate_ids);
    

    如果您想在一个盘子上附加许多文件:

    $plates->documents()->attach($document_ids);
    

    编辑:

    可能有一种更优雅的方式来执行此操作,而无需过多更改代码,但您可以执行以下操作:

    $plate_filter = [
        'plate_id_1' => 1,
        'plate_id_2' => 2,
        'plate_id_3' => 3,
    ];
    
    
    $doc_filter = [
        'recipe_id' => 1,
        'procedure_id' => 2,
    ];
    
    // attach many docs to plates
    $plates =   Plate::findMany($plate_filter);
    $docs   =   Document::findMany($doc_filter);
    
    // Loop over the selected plates, attach selected docs
    foreach( $plates as $plate ) {
    
        // $doc_ids = get your document id's here, use lists or something
        $plate->documents()->attach($doc_ids);
    }
    
    // Loop over the selected docs, attach selected plates
    foreach( $documents as $document ) {
    
        // You already have your plate id's in your first array,
        // simply use array_values to return a new array containing
        // only the plate id's
        $document->plates()->attach(array_values($plate_filter));
    }
    

    如果你可以将一个集合传递给 attach() 会很方便,我不确定你是否可以,而且我在 google 上找不到任何关于这样做的人的参考资料。

    【讨论】:

    • 我不明白这个。您能否编辑我上面的代码并将其显示给我。我需要先找到我假设的 id.. 对吗?
    • 我已经测试了你更新的代码。它有效,只有在第二个 foreeach 你会得到一个 Integrity constraint violation - 1062 Duplicate entry '1-1'
    • @user3641381 如果您阅读了我在第一个循环中所说的代码中制作的 cmets,您将必须获取要附加的文档的 ID。
    猜你喜欢
    • 2018-06-15
    • 2018-01-22
    • 2016-01-04
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多