Отношения Laravel принадлежат ToMany с составными первичными ключами

У меня есть 3 таблицы, и я пытаюсь установить отношения между order_products и order_products_status_names. У меня есть переходная / сводная таблица с именем order_product_statuses. Проблема в том, что у меня PK, потому что у меня в таблице 3 Pk, и я не знаю, как соединить эти 3 таблицы через отношения.
Мои миграции:

Стол заказов продукции:

 public function up()
{
Schema::create('order_products', function (Blueprint $table) {
$table->integer('order_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('ordinal')->unsigned();
$table->integer('size');

$table->primary(['order_id', 'product_id', 'ordinal']);
$table->foreign('order_id')->references('id')->on('orders');
$table->foreign('product_id')->references('id')->on('products');
});
}

Таблица Состояния продукта заказа — это моя переходная / сводная таблица между order_products и order_product_status_names

 public function up()
{
Schema::create('order_product_statuses', function (Blueprint $table) {
$table->integer('order_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('status_id')->unsigned();
$table->integer('ordinal')->unsigned();
$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

$table->foreign('order_id')->references('id')->on('orders');
$table->foreign('status_id')->references('id')->on('order_product_status_names');
$table->primary(['order_id', 'product_id', 'ordinal']);
});
}

И последний из них — Название статуса заказа.

public function up()
{
Schema::create('order_product_status_names', function (Blueprint $table) {
$table->integer('id')->unsigned();
$table->string('name');
$table->string('code');
$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

$table->primary('id');
});
}

Я знаю, что здесь есть связь blengsToMany двумя способами, но я не знаю или я могу объявить это отношение (от order_products до order_product_status_names и inverse)?

0

Решение

Хорошо, я не потратил на это много времени, но я бы так и сделал. Также, как упомянул @Devon, я бы, вероятно, добавил идентификаторы в каждую таблицу, поскольку Eloquent не предназначен для составных ключей. Как упоминалось в одном из моих комментариев, я обычно создаю сценарии запуска и обновления, поэтому синтаксис может быть не совсем правильным:

public function up() {
Schema::create('order_products', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->integer('order_id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('order_product_statuses_id')->unsigned();
$table->integer('ordinal')->unsigned();
$table->integer('size');

$table->primary('id');
$table->foreign('order_id')->references('id')->on('orders');
$table->foreign('product_id')->references('id')->on('products');
$table->foreign('order_product_statuses_id')->references('id')->on('order_product_statuses');
});
}

public function up() {
Schema::create('order_product_statuses', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->integer('product_id')->unsigned();
$table->integer('status_id')->unsigned();
$table->integer('ordinal')->unsigned();
$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

$table->primary('id');
$table->foreign('status_id')->references('id')->on('order_product_status_names');
});
}

public function up() {
Schema::create('order_product_status_names', function (Blueprint $table) {
$table->bigIncrements('id')->unsigned();
$table->string('name');
$table->string('code');
$table->dateTime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));

$table->primary('id');
});
}

HTH вы немного.

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector