【问题标题】:How to update existing and create new form fields in laravel如何在 laravel 中更新现有的表单字段并创建新的表单字段
【发布时间】:2020-02-23 18:11:49
【问题描述】:

我有一个将结构化数据保存到部分的表单,当我想编辑这些字段时遇到了一个棘手的情况。该表单具有动态字段,因此您可以在每个部分中添加任意数量的部分和字段。 我有一个主表,其中存储“例程”,它可以有多个“部分”,并且可以有多个“练习”条目。 现在我的解决方案是删除当前条目并将所有内容保存为全新的例程。

 public function update(Request $request, $id)
    {
        $user = auth()->user();
        $user->routines()->whereId($id)->delete();


        $requestArr = $request->all();
        $routine = new Routine();
        $routine->user_id = auth()->user()->id;
        $routine->name = $requestArr['name'];
        $routine->description = $requestArr['description'];
        $routine->save();

        $routine_id = $routine->id;

        foreach($requestArr['sections'] as $item){
            $section = new Section();
            $section->name = $item['name'];
            $section->description = $item['description'];
            $section->routine_id = $routine_id;
            $section->save();

            $section_id = $section->id;

            if( array_key_exists( 'exercises', $item ) ){
                foreach($item['exercises'] as $item2){
                    $exercise = new Exercise();
                    $exercise->name = $item2['name'];
                    $exercise->description = $item['description'];
                    $exercise->duration = 60;
                    $exercise->duration_unit = 'seconds';

                    $section->exercises()->save($exercise);
                }
            }
        }

        return redirect('/routines');
    }

我确信有一种更“优雅”的方式来做到这一点。

我的迁移: 例程:

Schema::create('routines', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('user_id');
    $table->string('name');
    $table->string('description');
    $table->timestamps();

    $table->foreign('user_id')
        ->references('id')
        ->on('users')
        ->onDelete('cascade');
});

部分:

   Schema::create('sections', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('routine_id');
        $table->string('name');
        $table->string('description');

        $table->timestamps();

        $table->foreign('routine_id')
            ->references('id')
            ->on('routines')
            ->onDelete('cascade');
    });

练习:

Schema::create('exercises', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('name');
    $table->string('description');
    $table->integer('duration');
    $table->string('duration_unit');

    $table->timestamps();


});

例程 - 一对多 - 部分 部分 - 多对多 - 练习(我有一个 exercise_section 中间表)

所以我的问题是,如何在不删除当前条目的情况下更新条目?因为万一添加了一些新的部分/练习,只有那些插入应该发生,所以只需 ->update();

【问题讨论】:

    标签: laravel


    【解决方案1】:

    您可以实施多种策略来实现这一目标。重新创建条目而不是更新它们是一项非常昂贵的任务,这可能会根据活动对您的服务产生瓶颈。

    例如,可以使用一些 if 子句来确定应该在 routine 条目上更新哪些字段,以检查某个值是否“脏”(即使 Eloquent 也提供了 some methods 这可以帮助您) .

    对于后续的相关条目(部分和练习),您需要实施一个涉及后端和前端的策略。例如,如果 update 方法正在接收新的和现有的 sections,您可以评估哪些没有 id 以确定需要创建哪些。

    但是,这是可以实施以避免重新创建条目的众多策略之一。可能看起来比较太多,但与重新创建所有条目相比,它会产生更便宜的工作量。

    【讨论】:

    • 是的,这是我的主要音乐会,因为当前的实施方式太“贪婪”了。就像一个想法一样,已经在前端包含一些信息是一种好的/可接受的做法,也许请求中的每个项目都有一个额外的字段可以告诉现有/新的? ir 可能包括 id 或类似的东西?
    • 虽然可以在前端包含一些额外的字段,但通常最好包含条目 id 以确定哪个是现有的/新的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2011-09-14
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多