代码之家  ›  专栏  ›  技术社区  ›  Code Lover

Laravel在透视表中为克隆字段的一个关系id插入多行

  •  0
  • Code Lover  · 技术社区  · 5 年前

    我有两个带pivot的关系表 routes , stations ,和 route_station 路线 表中包含地铁的路线号,以及 车站 包含所有电台。

    数据透视表 many-to-many one-to-many ?

    HTML表单

    网页上的HTML表单 Route 包含需要插入透视表的可关闭字段。见下文。

    • 车站
    • 距离

    问题

    所以我的问题是如何插入可关闭字段数据(在上面的 列表),以便在 枢轴 每个克隆字段的表?

    如果删除了clone字段,则从透视表中删除记录

    路由架构

    Schema::create(
        'routes',
        function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->smallInteger('number')->unsigned()->unique();
            $table->string('code')->unique();
            $table->timestamps();
    
            $table->unique(['number', 'code'], 'routes_unique_columns');
        }
    );
    

    Schema::create(
        'stations',
        function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->index()->unique();
            $table->string('code')->index()->unique();
            $table->text('info');
            $table->string('photo')->nullable();
            $table->timestamps();
        }
    );
    

    路由站(枢轴)架构

    Schema::create(
        'route_station',
        function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('route_id')->unsigned();
            $table->bigInteger('station_id')->unsigned();
            $table->bigInteger('next_station_id')->unsigned();
            $table->integer('station_order');
            $table->float('distance');
            $table->integer('duration');
            $table->timestamps();
    
            $table->unique(['route_id', 'station_id'], 'route_station_unique');
    
            $table->foreign('route_id')
                  ->references('id')
                  ->on('routes')
                  ->onDelete('restrict');
    
            $table->foreign('station_id')
                  ->references('id')
                  ->on('stations')
                  ->onDelete('restrict');
    
            $table->foreign('next_station_id')
                  ->references('id')
                  ->on('stations')
                  ->onDelete('restrict');
        }
    );
    

    路线创建/更新表单

    enter image description here

    我在下面试过,但不起作用。给 Array to String conversion attach sync 仅插入一条记录,而请求字段是一个数组。

    $route = Route::create([
        'number' => $request->number,
        'code'   => strtoupper($request->code),
    ]);
    
    $route->stations()->sync($route, [
        'next_station_id' => $request->next_station_id,
        'distance'        => $request->distance,
        'duration'        => $request->duration,
        'station_order'   => $request->station_order,
    ]);
    
    0 回复  |  直到 5 年前
        1
  •  1
  •   Kirmin    5 年前

    一个路段=>一个起点站,一个终点站。

    第二个线路段=>第二个起点站,第二个终点站。

    你可以一对一地考虑每条路线_station.station\u标识至站点id和路线_车站。下一个\u车站\u id至站点id

    因此,在route\u station表中,可以将station\u id字段与stations表建立一对一的关系,并将下一个station\u id字段与stations表建立一对一的关系

    另一方面,您有一条路线到多个路线站(多个路线段),这样您就可以选择一条路线的所有路线站。 因此,在routes表中,可以在字段上建立一对多关系路由.id与表路由站和野外路由_站点.路线id

    https://laravel.com/docs/7.x/eloquent-relationships#one-to-many

    确保选择了正确的laravel版本

    您只需要确保为每条记录的tge外部字段获得正确的id。

    您可以在此链接上阅读有关更新模型的更多信息 https://laravel.com/docs/7.x/eloquent#updates

    推荐文章