Laravel具有多个列的数据透视表,需要稍后插入 [英] Laravel pivot table with multiple columns that needs to insert later
问题描述
我有两个表routes
和stations
和一个数据透视表route_station
.查看表格详细信息
I am having two tables routes
and stations
and one pivot table route_station
. See table details
id, number, code
站台表
id, name, code
route_station表(枢轴)
id, route_id, station_id, next_station_id, interchange_station_id, sation_order, distance, duration
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');
}
);
Route_Station-枢轴
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.
- 电台页面
- 路线页面
- 我可以在其中添加站点以进行路由的页面
这意味着在创建station
或route
时,我不会将记录插入数据透视表中,而是在以后提到的第3页的任何时候.
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
并设置如下关系.
I have two models now Station
and Route
and set the relationship as below.
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');
}
}
所以现在的问题是,我不知道在station
和route
记录已经存在的情况下,如何将记录插入到数据透视表中以填充所有列到role_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.
我尝试使用attach()
,但是它给出了以下错误
I have tried to use attach()
but it is giving below error
带有消息'SQLSTATE [HY000]的Illuminate/Database/QueryException: 常规错误:1364字段"station_order"没有默认值 (SQL:插入
route_station
(route_id
,station_id
)值 (2,4))'
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))'
所以我需要帮助
问题:
我了解了要求我设置
station_order
列值的错误,但我不知道如何传递next_station_id
,interchange_station_id
,station_order
,distance
,
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
推荐答案
默认情况下,仅模型键会出现在pivot
对象上.如果数据透视表包含额外的属性,则在定义关系时必须指定它们:
By default, only the model keys will be present on the pivot
object. 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');
这篇关于Laravel具有多个列的数据透视表,需要稍后插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!