У меня две таблицы routes
и stations
и одна сводная таблица route_station
. См. Подробности таблицы
таблица маршрутов
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
Схема
Станция
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 - Pivot
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');
}
);
В административной области будет всего три раздела для управления станциями и маршрутами.
- Страница станций
- Страница маршрутов
- Страница, на которой я могу добавить станции к маршруту
Это означает, что я не собираюсь вставить запись в сводную таблицу при создании station
или route
, но в любое время позже из упомянутой выше 3-й страницы.
Теперь у меня есть две модели 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');
}
}
Так что теперь проблема в том, что я не знаю, как вставить запись в сводную точку таблица для заполнения всех столбцов в таблице role_station
, в то время как записи station
и route
уже существуют.
Я пытался использовать attach()
, но выдает ошибку ниже
Подсветка / База данных / QueryException с сообщением «SQLSTATE [HY000]: общая ошибка: 1364 Поле« station_order »не имеет значения по умолчанию (SQL: вставить в route_station
(route_id
, station_id
») ) значения (2, 4)) '
Так что мне нужна помощь с этим
Вопрос:
Мне недоставало d ошибка, которая просит меня установить значение столбца station_order
, но я> не знаю, как я могу передать значения для next_station_id
, interchange_station_id
, station_order
, distance
, duration