[英]Laravel pivot table with multiple columns that needs to insert later
I am having two tables routes
and stations
and one pivot table route_station
.我有两张桌子
routes
和stations
和一张数据透视表route_station
。 See table details查看表格详情
id, number, code
id, name, code
id, route_id, station_id, next_station_id, interchange_station_id, sation_order, distance, duration
station_id
, next_station_id
, interchange_station_id
all are the satations
table id station_id
, next_station_id
, interchange_station_id
一切都是satations
表ID
Schema::create(
'stations',
function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name')->index();
$table->string('code')->index();
$table->text('info');
$table->string('photo')->nullable();
$table->timestamps();
}
);
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(
'route_station',
function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('route_id')->unsigned();
$table->bigInteger('station_id')->unsigned();
$table->bigInteger('next_station_id')->unsigned()->nullable();
$table->bigInteger('interchange_id')->unsigned()->nullable();
$table->integer('station_order');
$table->float('distance');
$table->integer('duration');
$table->timestamps();
$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');
$table->foreign('interchange_id')
->references('id')
->on('routes')
->onDelete('restrict');
}
);
In the admin area, there will be a total of three sections to manage stations and routes.在管理区域,总共将有三个部分来管理车站和路线。
That means I am not going to insert a record into pivot while creating a station
or route
but at any time later from the 3rd-page mention above.这意味着我不会在创建
station
或route
时将记录插入到枢轴中,而是在上面提到的第 3 页之后的任何时间。
I have two models now Station
and Route
and set the relationship as below.我现在有两个模型
Station
和Route
并将关系设置如下。
class Station extends Model
{
public function routes()
{
return $this->belongsToMany('App\Route');
}
public function route()
{
return $this->belongsTo('App\Route');
}
}
class Route extends Model
{
public function stations()
{
return $this->belongsToMany('App\Station');
}
public function station()
{
return $this->belongsTo('App\Station');
}
}
So now the problem is that I don't know how to insert a record into the pivot table to fill out all the columns into the role_station
table while station
and route
record already exists.所以现在的问题是,当
station
和route
记录已经存在时,我不知道如何将记录插入到数据透视表中以将所有列填充到role_station
表中。
I have tried to use attach()
but it is giving below error我曾尝试使用
attach()
但它给出了以下错误
Illuminate/Database/QueryException with message 'SQLSTATE[HY000]: General error: 1364 Field 'station_order' doesn't have a default value (SQL: insert into
route_station
(route_id
,station_id
) values (2, 4))'照射/数据库/ QueryException与消息'SQLSTATE [HY000]:常规错误:1364字段'station_order'不具有默认值(SQL:INSERT INTO
route_station
(route_id
,station_id
)值(2,4))'
So I need help with this所以我需要帮助
Question:
题:
I understood the error that is asking me to set the
station_order
column value but I don't >know how can I pass values fornext_station_id
,interchange_station_id
,station_order
,distance
,duration
我理解要求我设置
station_order
列值的错误,但我不知道如何传递next_station_id
、interchange_station_id
、station_order
、distance
、duration
By default, only the model keys will be present on the pivot
object.默认情况下,只有模型键会出现在
pivot
对象上。 If your pivot table contains extra attributes, you must specify them when defining the relationship:如果数据透视表包含额外的属性,则必须在定义关系时指定它们:
return $this->belongsToMany('App\Route')->withPivot('next_station_id', 'interchange_station_id', 'station_order', 'distance', 'duration');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.