【发布时间】: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
sync 和 attach 方法。
上面的代码产生错误
调用未定义的方法 Illuminate\Database\Eloquent\Collection::documents()
任何想法我做错了什么?我这样做对吗?
【问题讨论】: