我有两个带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');
}
);
路线创建/更新表单
我在下面试过,但不起作用。给
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,
]);