为什么这种关系不起作用?
我正在尝试使用 laravel 5.2、mysql、migrations 和 seeders 关联帖子和类别。
但我收到一个错误:
调用未定义的方法 Illuminate\Database\Eloquent\Relations\BelongsTo::attach()
PostTableSeeder.php
public function run()
{
factory(App\Post::class, 300)->create()->each(function (App\Post $post) {
$post->category()->attach([
rand(1, 5),
rand(6, 14),
rand(15, 20),
]);
});
}
模型: Post.php
public function category()
{
return $this->belongsTo(Category::class);
}
模型: Category.php
public function posts()
{
return $this->belongsTo(Post::class);
}
30秒到达战场