使用同步保存多对多(多态)关系

时间:2018-12-15 07:13:31

标签: php laravel eloquent

您能帮我吗,如何使用同步方法保存多对多(多态)?

表格结构示例 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);
});

0 个答案:

没有答案