您能帮我吗,如何使用同步方法保存多对多(多态)?
表格结构示例 enter image description here
示例代码
factory(Post::class, 50)->create()->each(function($tag) use($faker) {
$randomClass = $faker->randomElement([Post::class, Video::class]);
$img_id = ($randomClass)::all()->random()->id;
$tagIds = ($randomClass)::inRandomOrder()->take(rand(1,5))->pluck('id');
(new $randomClass)->tags()->sync($tagIds);
});