重构模型数据透视表插入

时间:2018-08-06 05:17:26

标签: php laravel eloquent

我如何重构这样的东西?

我将单个文件的数组发送到此控制器方法,每个文件都有团队成员,标签等

collect(request('singles'))->each(function ($single) {

    $model = auth()->user()->singles()->create([
        'title' => $single['title'],
        'src' => $single['src']
    ]);

    if (isset($single['team'])) {
        foreach ($single['team'] as $member) {
            $model->team()->create(['user_id' => $member]);
        }
    }

if (isset($single['tags'])) {
        foreach ($single['tags'] as $tag) {
            $model->tags()->create(['tag_name' => $tag]);
        }
    }


if (isset($single['faces'])) {
        foreach ($single['faces'] as $face) {
            $model->faces()->create(['face_data' => $face]);
        }
    }

});

我想为每个文件插入更新许多表,就性能和可读性而言,什么是最好的方法?有什么帮助吗?

0 个答案:

没有答案