Модель Laravel Eloquent: 2 поля, один FK - 2 поля в другой таблице - PullRequest
0 голосов
/ 20 марта 2019

Я хочу использовать модели и отношения Laravel Eloquent для объединения данных из всех таблиц в моем проекте;Однако у меня есть проблема с переводом этих отношений.Например, у меня есть две таблицы;первая - это таблица книг, а вторая - таблица авторов.

Schema::create('books', function (Blueprint $table) {
    $table->increments('id');
    $table->string('code', 20)->unique('ak_books__code')->nullable(false);
    $table->smallInteger('books_type_id');
    $table->float('detection_limit', 10, 0);
    $table->smallInteger('books_classification_id');
    $table->smallInteger('books_number');
    $table->foreign(['books_classification_id', 'books_number'], 'fk_books__classification_scales')
        ->references(['calibration_id', 'number'])->on('classification')
        ->onUpdate('RESTRICT')->onDelete('RESTRICT');
});

Schema::create('classification', function (Blueprint $table) {
    $table->smallInteger('calibration_id');
    $table->smallInteger('number');
    $table->string('name', 50);
    $table->primary(['calibration_id', 'number'], 'pk_classification_scales');
    $table->foreign('calibration_id', 'fk_classification_calibration')->references('id')
        ->on('calibration_parameters')->onUpdate('CASCADE')->onDelete('CASCADE');
});

Как мне установить отношения на таблице книг, чтобы взять number и calibration_id?

1 Ответ

0 голосов
/ 20 марта 2019

Как и в случае с классификацией книг, я покажу вам пример связи между заданиями-проектами (каков проект этой задачи):

ЗАДАЧА

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('tasks', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->text('description');
        $table->string('name');
        $table->integer('project_id')->unsigned();
        $table->timestamp('start_date');
        $table->timestamp('end_date');
        $table->timestamps();

        $table->foreign('project_id')->references('id')->on('projects')->onDelete('cascade')->onUpdate('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('tasks');
}

ПРОЕКТЫ

/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('projects', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('description');
        $table->tinyInteger('active')->default(1);
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('projects');
}

Надеюсь, тебе это поможет, привет!

...